Date format Conversion

J

John Smith

I need to convert "20050930" to "9/30/2005 12:00:00.000 AM" using Java
Strings and (perhaps?) regular expressions.

The idea is to match 2005 as the year, and put it under the [yyyy] part
of mm/dd/yyyy, then match the next 2 digits of mm, then the next 2
digits of dd, etc. I need to scan the "20050930" string 1 character at
a time.

Should I use regular expressions? Or is there a simpler method?

Examples will also be appreciated.
 
C

camiller

Here is an example I had handy where I am parsing out a date in the
format MM/DD/YYYY to get the parts. You should be able to adapt it to
your use. dateField, day, month, and year are Strings:

day = dateField.substring(3,5);
month = dateField.substring(0,2);
year = dateField.substring(6,10);
 
I

IchBin

John said:
I need to convert "20050930" to "9/30/2005 12:00:00.000 AM" using Java
Strings and (perhaps?) regular expressions.

The idea is to match 2005 as the year, and put it under the [yyyy] part
of mm/dd/yyyy, then match the next 2 digits of mm, then the next 2
digits of dd, etc. I need to scan the "20050930" string 1 character at
a time.

Should I use regular expressions? Or is there a simpler method?

Examples will also be appreciated.
How about something like this:

Format formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS aaa");
String var = "20050930";
String formattedDate = formatter.format(
new Date(var.substring(4,6)
+"/"+var.substring(6,8)
+"/"+var.substring(0,4)));
--

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
R

Roedy Green

The idea is to match 2005 as the year, and put it under the [yyyy] part
of mm/dd/yyyy, then match the next 2 digits of mm, then the next 2
digits of dd, etc. I need to scan the "20050930" string 1 character at
a time.

Hava a look at SimpleDateFormat.parse

Those jobs I am lazy and tend do the code with character handling
rather than try to decode somebody's documentation on how to use their
more complex tool.

It may or may not handle variable width fields. Let us know what you
find out.
 
T

tom fredriksen

Here is an example I had handy where I am parsing out a date in the
format MM/DD/YYYY to get the parts. You should be able to adapt it to
your use. dateField, day, month, and year are Strings:

day = dateField.substring(3,5);
month = dateField.substring(0,2);
year = dateField.substring(6,10);

That only works if you know the string to be of the exact format, if its
not then you have problems. so you need to add some code to verify the
data before it can be accepted. This code is already invented in f.ex in

SimpleDateFormat

/tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top