?
Solved

Date Format

Posted on 2011-02-17
34
Medium Priority
?
972 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 1000 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 1000 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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Upgrading Tomcat – There are a couple of methods to upgrade Tomcat is to use The Apache Installer is to download and unzip and run the services.bat remove|install Tomcat6 Because of the App that we are working with, we can only use Tomcat 6.…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month8 days, 7 hours left to enroll

764 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