• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 980
  • Last Modified:

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
0
Navarre_EDI
Asked:
Navarre_EDI
  • 21
  • 10
  • 2
  • +1
2 Solutions
 
for_yanCommented:
You can use SimpleDateFormat class - which is very flexible in defining how to print date
0
 
Navarre_EDIAuthor Commented:
Can you send me code to do this?
0
 
for_yanCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
for_yanCommented:

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
 
Navarre_EDIAuthor Commented:
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
 
for_yanCommented:
You probably need to import:

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

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

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
 
for_yanCommented:
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
 
CEHJCommented:
>>in and want to convert to 2011218 or 20111118

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

to
20110211

0
 
for_yanCommented:
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
 
for_yanCommented:
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
 
Navarre_EDIAuthor Commented:
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
 
for_yanCommented:

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
 
for_yanCommented:
Sorry, if value is the date, then it is different
0
 
for_yanCommented:
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
 
for_yanCommented:
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
 
Navarre_EDIAuthor Commented:
Once I ran your script it return this
2/18/201

again it is coming in as 2/18/2011

I want
20110218
0
 
for_yanCommented:
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
 
Navarre_EDIAuthor Commented:
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
 
for_yanCommented:
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
 
for_yanCommented:
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
 
Navarre_EDIAuthor Commented:
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
 
for_yanCommented:
Are you using Java? Or is it Basic or JavaScript?
0
 
Navarre_EDIAuthor Commented:
Java
0
 
CEHJCommented:
>> 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
 
for_yanCommented:
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
 
Navarre_EDIAuthor Commented:
Java Script sorry for yan .
0
 
objectsCommented:
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
 
for_yanCommented:

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 21
  • 10
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now