Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with simple date format class

 
Ram Korutla
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using the below code snippet. When I am using 201230, the year being returned is 2020. If I input 301230, it gives 1930. I am expecting 1920 similarly for 201230 input also. Can some one please clarify, what I need to do to ensure I get 1920 instead of 2020? Also, the other issue, is how does java know whether it is a future date input or input of past date?

Sorry for this simple issue.



Thanks
Ram
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the SimpleDateFormat specification:
For parsing with the abbreviated year pattern ("y" or "yy"), SimpleDateFormat must interpret the abbreviated year relative to some century. It does this by adjusting dates to be within 80 years before and 20 years after the time the SimpleDateFormat instance is created.

So for "30" to parse as "2030", just wait about three weeks! More generally, if you want the year to always be parsed as this century, you might want to insert a "20".
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic