aspose file tools*
The moose likes Java in General and the fly likes Parsing a string with SimpleDateFormatter always give January as month Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Parsing a string with SimpleDateFormatter always give January as month" Watch "Parsing a string with SimpleDateFormatter always give January as month" New topic
Author

Parsing a string with SimpleDateFormatter always give January as month

Derek Boring
Ranch Hand

Joined: Aug 15, 2006
Posts: 63
Hello Friends,

I'm having an issue that several hours of google searching and tweaking has been unable to resolve. I'm reading an XML file using Java DOM. That part is working correctly. Each element within the file contains a date attribute that is in the format of "MM/DD/yyyy" e.g. "07/23/2013" I am trying to use a SimpleDateFormat object to parse the string into a Calendar object. It correctly reads the day and year into the Calendar object, but always returns January (1) as the month. Here is the code I am using:



The SimpleDateFormat object's parse method returns a Date object, so here is my method to convert the Date object to a Calendar object:



Is there something I am missing or doing incorrectly?

EDIT:
I added the following line to the dateToCalendar method just to see what the output would be:



It prints out 0 when the month in the input string is "07". So it seems to me that the problem is with the SimpleDateFormat object and not the Date or Calendar objects, and not with the dateToCalendar method. Any ideas?


Life is 10% what happens and 90% how you react to it.
Attitude is everything.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Derek Boring wrote:
It prints out 0 when the month in the input string is "07". So it seems to me that the problem is with the SimpleDateFormat object and not the Date or Calendar objects, and not with the dateToCalendar method. Any ideas?


Hint: What does "DD" mean in the date format?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Derek Boring
Ranch Hand

Joined: Aug 15, 2006
Posts: 63
Oh my gosh!!!


How on earth did I miss that?!?


Thank you so much!!!


You are the best! I can't believe I didn't pick up on that. Thank you again!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18658
    
    8

Nasty things, those date formats. Once I used HH instead of hh -- or was it the other way around? -- and I ended up with code which worked in the morning but not in the afternoon. (Or maybe the other way around.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Parsing a string with SimpleDateFormatter always give January as month