Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parsing a string with SimpleDateFormatter always give January as month

 
Derek Boring
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Henry Wong
author
Marshal
Pie
Posts: 21122
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Derek Boring
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic