• Post Reply Bookmark Topic Watch Topic
  • New Topic

SimpleDateFormat -->2 Year Dates  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't seem to get the SimpleDateFormat.set2DigitYearStart method to work as advertised.
Does anyone have an example of using this?
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
J Menke
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim,
It appears that the process of parsing dates is not automatic. You have to know if it's 2 digits or 4 digits. I was trying to use the set2DigitYearStart with SimpleDateFormat that was formatted to MM/dd/yyyy... it wasn't working.
What i'm trying to do is allow my users to enter a 2 digit year or 4 digit year and handle both correctly. Do you have any ideas on this?
-jm
[ May 16, 2003: Message edited by: J Menke ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you could try looking at the length() of the String before you try to format it. But that's probably not good enough - e.g. 5/1/2003 and 05/01/03 have the same length, but different formats. Probably the simplest technique is to use exception handling:

If the first parse() throws an exception, try again; if the second fails too, let the exception propagate to wherever you want to catch it.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!