This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
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 Building Microservices this week in the Design 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: 65
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: 20057
    
  44

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: 65
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
Sheriff

Joined: Oct 14, 2005
Posts: 19728
    
  10

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.)
 
Have you checked out Aspose?
 
subject: Parsing a string with SimpleDateFormatter always give January as month
 
jQuery in Action, 3rd edition