Solved

Date Format

Posted on 2011-02-17
34
939 Views
Last Modified: 2012-05-11
Hello

I have a string coming in as 2/18/2011 or 11/18/2011 coming in and want to convert to
2011218 or 20111118
0
Comment
Question by:Navarre_EDI
  • 21
  • 10
  • 2
  • +1
34 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 34916546
You can use SimpleDateFormat class - which is very flexible in defining how to print date
0
 

Author Comment

by:Navarre_EDI
ID: 34916582
Can you send me code to do this?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34916633
SimpleDateFormat f1 = new SimpleDateFormat("MM/dd/yyyy");
SimpleDtaeFormat f2 = new SimpleDateFormat("yyyyDDD");

String s1 = "10/12/2011";

java.util.Date d = f1.parse(s1, new ParsePosition(0));

String s2 = f2.format(d);
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34916677

Some more examples of how to use SimpleDateFormat:
http://www.expert.tc/topic.php?id=103726

In the code I wrote above - it needs to be checked with one digit and two digit months, it still
works in most cases, in the wors case you may check the lenth of the string, I'll try too do it now
0
 

Author Comment

by:Navarre_EDI
ID: 34916698
This does not work getting an error

// Declare and initialize this link's instance variables:

// Define this link's methods:
import java.text.SimpleDateFormat;
public String doFirst(String value)
{
   return value;
}

public String doEach(String value)

{
   SimpleDateFormat f1 = new SimpleDateFormat("MM/dd/yyyy");
SimpleDtaeFormat f2 = new SimpleDateFormat("yyyyDDD");

String s1 = "10/12/2011";

java.util.Date d = f1.parse(s1, new ParsePosition(0));

String s2 = f2.format(d);
   return s2;

}

public String doLast(String value)
{
   return value;
}




WARN  02/17/11 09:06:31 - Syntax errors...
   Cannot find class "SimpleDateFormat" [JLS 8]
... end syntax errors. [methodseditor.MethodsEditor$LocationUpdater]
WARN  02/17/11 09:09:33 - Syntax errors...
   Cannot find class "SimpleDtaeFormat" [JLS 8]
... end syntax errors. [methodseditor.MethodsEditor$LocationUpdater]
WARN  02/17/11 09:10:00 - Syntax errors...
   Syntax error: unexpected token: import
... end syntax errors. [methodseditor.MethodsEditor$LocationUpdater]
INFO  02/17/11 09:11:09 - Workspace Saved: C:\Program Files\TIE Commerce\Integrator_5.3\AUDYSSEY.workspace [action.SaveObjectAction]
WARN  02/17/11 09:11:11 - Syntax errors...
   Cannot find class "SimpleDtaeFormat" [JLS 8]
... end syntax errors. [methodseditor.MethodsEditor$LocationUpdater]
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34916733
You probably need to import:

 import java.text.SimpleDateFormat;
   import java.text.ParsePosition;

It works since Java 1.4 I believe
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34916782
This exact code actually works for me with any number of digist:

say 2/11/2011 and 2/2/2011 - all are ok, only in the result it adds zero in number of days - say
2011033
You can of course check and modify it in  the resultsing string if you want 201133 in such case

But its part of Java JDK since 1.4 so you shhould find ythe class
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34916801
Yes, correct my spelling over there iuin your code - always SimpleDateFormat - not SimpleDtaeFormat as I typed quickly
0
 

Author Comment

by:Navarre_EDI
ID: 34916838
Error

INFO  02/17/11 08:57:12 - ******************************************************************************** [action.RunAction]
INFO  02/17/11 08:57:12 - Return Code from run: 0 [action.RunAction]
WARN  02/17/11 09:06:31 - Syntax errors...
   Cannot find class "SimpleDateFormat" [JLS 8]
... end syntax errors. [methodseditor.MethodsEditor$LocationUpdater]
WARN  02/17/11 09:09:33 - Syntax errors...
   Cannot find class "SimpleDtaeFormat" [JLS 8]
... end syntax errors. [methodseditor.MethodsEditor$LocationUpdater]
WARN  02/17/11 09:10:00 - Syntax errors...
   Syntax error: unexpected token: import
... end syntax errors. [methodseditor.MethodsEditor$LocationUpdater]
INFO  02/17/11 09:11:09 - Workspace Saved: C:\Program Files\TIE Commerce\Integrator_5.3\AUDYSSEY.workspace [action.SaveObjectAction]
WARN  02/17/11 09:11:11 - Syntax errors...
   Cannot find class "SimpleDtaeFormat" [JLS 8]
... end syntax errors. [methodseditor.MethodsEditor$LocationUpdater]
WARN  02/17/11 09:22:15 - Syntax errors...
   Cannot find class "SimpleDtaeFormat" [JLS 8]
... end syntax errors. [methodseditor.MethodsEditor$LocationUpdater]
INFO  02/17/11 09:22:25 - Workspace Saved: C:\Program Files\TIE Commerce\Integrator_5.3\AUDYSSEY.workspace [action.SaveObjectAction]
WARN  02/17/11 09:22:28 - Syntax errors...
   Cannot find class "SimpleDtaeFormat" [JLS 8]
... end syntax errors. [methodseditor.MethodsEditor$LocationUpdater]
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34916896

Which version of java are you using?
Are you using some IDE environment?
Did you add import statements in your code?

Of course correct this "Dtae" to "Date" in SimpleDtaeFormat in the second case (declaration of f2)

With orrect spelling and import it must work with Java not earlier than 1.4
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34916924
imports are usually at the very top of the code , before class, I'm not sure they
would work before method declaration inside the class
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34916987
>>in and want to convert to 2011218 or 20111118

Better to use the ISO format (2011-11-18) if you can
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34917090
I actually thought the author wants number of days in the year in the output,
if it is year-month-day(in the month), then it would be this way:


   SimpleDateFormat f1 = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat f2 = new SimpleDateFormat("yyyy-MM-dd");

String s1 = "10/12/2011";

java.util.Date d = f1.parse(s1, new ParsePosition(0));

String s2 = f2.format(d);
   return s2;


In any case SimpleDateFormat is part of the standard Java
and it must be found if the JDK is correctly installed and configured.
0
 

Author Comment

by:Navarre_EDI
ID: 34918130
All I want to do is change the date from
2/11/2011
or
02/11/2011

to
20110211

0
 
LVL 47

Expert Comment

by:for_yan
ID: 34918154
Well if course you can do it in many different ways, I'll show you some in
a minute, but this SimpleDtaeFormat shoud work - there is no reason for it not to work.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34918228
Below is probably the longest way.
You can also use split - but if you don't have SimpleDateFormat it may not work either,
substring should work always

String s1 = "2/11/2011";

String ss0 = s1.substring(0,s1.indexOf("/"));

String ss1 = s1.substring(s.indexOf("/")+1,s.lastIndexOf("/"));

String ss2 = s1.substring(s.lastIndexOf("/")+1);

while(ss0.length()<2)ss0="0"+ss0;

while(ss1.length()<2)ss1="0" +ss1;

String s2 = ss2 + ss0 + ss1;


0
 

Author Comment

by:Navarre_EDI
ID: 34918290
Can you please explain how to put it in the function

value is the date being passed to me


public String doEach(String value)

{
   String ss0 = s1.substring(0,s1.indexOf("/"));

   String ss1 = s1.substring(s.indexOf("/")+1,s.lastIndexOf("/"));

   String ss2 = s1.substring(s.lastIndexOf("/")+1);

   while(ss0.length()<2)ss0="0"+ss0;

   while(ss1.length()<2)ss1="0" +ss1;

   String s2 = ss2 + ss0 + ss1;
   return s2;

}
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 47

Expert Comment

by:for_yan
ID: 34918338

You had it almost right, i just added one lineString  s1 = value;
public String doEach(String value)

{
   String s1 = value;
   String ss0 = s1.substring(0,s1.indexOf("/"));

   String ss1 = s1.substring(s.indexOf("/")+1,s.lastIndexOf("/"));

   String ss2 = s1.substring(s.lastIndexOf("/")+1);

   while(ss0.length()<2)ss0="0"+ss0;

   while(ss1.length()<2)ss1="0" +ss1;

   String s2 = ss2 + ss0 + ss1;
   return s2;

}
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34918346
Sorry, if value is the date, then it is different
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34918428
If value is the date, then
it is something like below, but let me test in the real code:

public String doEach(java.util.Date d){


String s0 =  "" + (d.getYear() + 1900) ;
String s1 = "" + d.getDate();
String s2 = "" + (d.getMonth()+1);

 while(s1.length()<2)s1="0"+s1;

   while(s2.length()<2)s2="0" +s2;


return (s0+s1+s2);


}
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34918500
Yes, it works but the order in the return should be changed like that
if you want months first (see below).

Compiler will probably write you that all these methods are deprecated -
don't worry.  Nowadays you should use Calendar,
but if you don't have SimpleDateFormat, then you'll not have Calendar even more probably.
Deprected are all still working fine, so don't worry about it

public String doEach(java.util.Date d){


String s0 =  "" + (d.getYear() + 1900) ;
String s1 = "" + d.getDate();
String s2 = "" + (d.getMonth()+1);

 while(s1.length()<2)s1="0"+s1;

   while(s2.length()<2)s2="0" +s2;


return (s0+s2+s1);


}
0
 

Author Comment

by:Navarre_EDI
ID: 34918896
Once I ran your script it return this
2/18/201

again it is coming in as 2/18/2011

I want
20110218
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34918966
No, it cannot be - it returns fine for me - send me the ppiece of code where you are running and where you are printing
priniting - you are prtobably printing something else - oriiginal value - there is nowhere it can get slashes in the retiurn
0
 

Author Comment

by:Navarre_EDI
ID: 34919176
Here is the code
// Declare and initialize this link's instance variables:

// Define this link's methods:



public String doFirst(String value)
{
   return value;
}


public String doEach(java.util.Date d){


      String s0 =  "" + (d.getYear() + 1900) ;
      String s1 = "" + d.getDate();
      String s2 = "" + (d.getMonth()+1);

       while(s1.length()<2)s1="0" +s1;
       while(s2.length()<2)s2="0" +s2;
      return (s0+s2+s1);

}

public String doLast(String value)
{
   return value;
}


Output

2/18/201
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34919751
This code does not have the output opertaion - at what point and where you print it?
There should be something like System.out.prinltn(...)

This just a bunch of methods and it does not say when these methods are called and what is printed
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34919767
This code by itself would not print anything - send me your full code or locate the
place  where you call this method and the place where you print
0
 

Author Comment

by:Navarre_EDI
ID: 34919842
the return should send me a value.

Here is the code I have right now it works but not for like if day was 1 char or month was 1 char

// Declare and initialize this link's instance variables:

// Define this link's methods:
public String doFirst(String value)
{
   return value;
}

public String doEach(String value)
{
     return Function.Mid(value, 6, 4) + Function.Left(value, 1) + Function.Mid(value, 3, 2);
}

public String doLast(String value)
{
   return value;
}


the value that it returns is 2011218
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34919884
Are you using Java? Or is it Basic or JavaScript?
0
 

Author Comment

by:Navarre_EDI
ID: 34919921
Java
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34919924
>> String s0 =  "" + (d.getYear() + 1900) ;

You certainly shouldn't be using deprecated methods. If you want a different way of doing it, try
String date = "11/18/2011";
	String[] atoms = date.split("/");
	Calendar c = new GregorianCalendar(Integer.valueOf(atoms[2]), Integer.valueOf(atoms[0]), Integer.valueOf(atoms[1]));
	String conv = String.format("%04d%02d%02d", c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE)); 
	System.out.println(conv);

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 34919954
No you are not using Java -  Function.Mid(value, 6, 4) - is not from Java;
It is probably JavaScript

@CEHJ
They don't have SimpleDateFormat , they would even less have Calendar
I think it is not Java at all
0
 

Author Comment

by:Navarre_EDI
ID: 34919984
Java Script sorry for yan .
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 34920028
You should be using a SimpleDateFormat as for_yan suggested

http://helpdesk.objects.com.au/java/how-do-i-parse-a-java-string-that-specifies-a-date
http://helpdesk.objects.com.au/java/how-do-i-format-a-date-as-a-string


public String doEach(String value) {
   SimpleDateFormat f1 = new SimpleDateFormat("MM/dd/yyyy");
   SimpleDateFormat f2 = new SimpleDateFormat("yyyyMDD");
   return f2.format(f1.parse(value));
}
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 250 total points
ID: 34920044

Maybe this would work for you:
(don/t know envoug of JavaScript
and should not be public String - in javascript.
Anyway you may try:

public String doEach(String value)
{
    var s1 = Function.Mid(value, 6, 4);
    var s2 = Function.Left(value, 1);
    var s3 = Function.Mid(value, 3, 2);
 
   while(s2.length< 2)s2 = "0" +s2;
while(s3.length < 2)s3 = "0" +s3;

return s1 + s2 + s3;

   
}


0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Verbose logging is used to diagnose garbage collector problems. By default, -verbose:gc output is written to either native_stderr.log or native_stdout.log.   It is also possible to redirect the logs to a user-specified file. This article will de…
Most of the developers using Tomcat find it easy to configure the datasource in Server.xml and use the JNDI name in the code to get the connection.  So the default connection pool using DBCP (or any other framework) is made available and the life go…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now