Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Date format Conversion

Thread view: 
John Smith - 22 Feb 2006 14:12 GMT
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.
Paul Hamaker - 22 Feb 2006 14:43 GMT
Simpler method ? Yes,
http://javalessons.com/cgi-bin/ui/java-swing.cgi?1cd=dte&sid=ao789
line 22
camiller@gmail.com - 22 Feb 2006 14:45 GMT
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);
tom fredriksen - 23 Feb 2006 10:46 GMT
> 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
[quoted text clipped - 3 lines]
> 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
IchBin - 22 Feb 2006 15:25 GMT
> I need to convert "20050930" to "9/30/2005 12:00:00.000 AM" using Java
> Strings and (perhaps?) regular expressions.
[quoted text clipped - 7 lines]
>
> 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)));
Signature


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

Roedy Green - 22 Feb 2006 17:27 GMT
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.