Link to home
Start Free TrialLog in
Avatar of Navarre_EDI
Navarre_EDI

asked on

Date Format

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
Avatar of for_yan
for_yan
Flag of United States of America image

You can use SimpleDateFormat class - which is very flexible in defining how to print date
Avatar of Navarre_EDI
Navarre_EDI

ASKER

Can you send me code to do this?
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);

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
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]
You probably need to import:

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

It works since Java 1.4 I believe
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
Yes, correct my spelling over there iuin your code - always SimpleDateFormat - not SimpleDtaeFormat as I typed quickly
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]

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
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
Avatar of CEHJ
>>in and want to convert to 2011218 or 20111118

Better to use the ISO format (2011-11-18) if you can
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.
All I want to do is change the date from
2/11/2011
or
02/11/2011

to
20110211

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.
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;


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;

}

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;

}
Sorry, if value is the date, then it is different
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);


}
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);


}
Once I ran your script it return this
2/18/201

again it is coming in as 2/18/2011

I want
20110218
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
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
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
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
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
Are you using Java? Or is it Basic or JavaScript?
Java
>> 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

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
Java Script sorry for yan .
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial