• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Parsing a string with SimpleDateFormatter always give January as month

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic