File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to convert 4 strings MMDDCCYY to a date object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to convert 4 strings MMDDCCYY to a date object?" Watch "How to convert 4 strings MMDDCCYY to a date object?" New topic
Author

How to convert 4 strings MMDDCCYY to a date object?

Reed Peters
Ranch Hand

Joined: Jul 23, 2002
Posts: 38
Hi,
I am accessing an old database via SQL. The database stores dates in four different fields-Month, Day, Century, and Year- each are two digits. What is the best approach to use to convert these into a Date object? Also, I noticed that I am not getting the leading zeros either, so july would be a 7 instead of 07.
Thanks in advance.
[ October 25, 2002: Message edited by: Reed Peters ]

Reed Peters<br />Reed@cm-inc.com
San Su
Ranch Hand

Joined: Jul 06, 2001
Posts: 313
Calendar cal = Calendar.getInstance().
cal.set(Calendar.DATE, nDate);
cal.set(Calendar.MONTH, nMonth);
cal.set(Calendar.YEAR, nCentury + nYear);
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
Does this help? Sorry it is a quick response. Forgive me if it doesnt work. There are several other approaches.
[ October 25, 2002: Message edited by: Sankar Subbiah ]
Reed Peters
Ranch Hand

Joined: Jul 23, 2002
Posts: 38
Thank you Sankar. That worked great.
yi zhu
Ranch Hand

Joined: Sep 10, 2002
Posts: 41
Also month begins with 0
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

You can use SimpleDateFormat to parse Strings to Dates, depending on their String pattern:
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Also, just beware of this hidden detail:

Month Mappings
mon -> int -> Calendar field
Jan -> 0 -> Calendar.JANUARY
Feb -> 1 -> Calendar.FEBRUARY
Mar -> 2 -> Calendar.MARCH
...
Dec -> 11 -> Calendar.DECEMBER

Just a heads up on this, I know it caused me unforseen problems before!
Jamie
[I think that is what Yi briefly implied]
[ October 29, 2002: Message edited by: Jamie Robertson ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to convert 4 strings MMDDCCYY to a date object?