Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control SimpleDateFormat parse to 19xx or 20xx?

Tags:

Is there a way to parse the following date string as July 23 1916 rather than July 23 2016?

System.out.println(new SimpleDateFormat("yy/MM/dd", Locale.US).parse("16/07/23")); 
like image 826
Jason Avatar asked Jul 23 '14 06:07

Jason


Video Answer


1 Answers

The Java Doc (http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) says:

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. For example, using a pattern of "MM/dd/yy" and a SimpleDateFormat instance created on Jan 1, 1997, the string "01/11/12" would be interpreted as Jan 11, 2012 while the string "05/04/64" would be interpreted as May 4, 1964.

The method SimpleDateFormat.set2DigitYearStart(Date) can be used to fix the year.

like image 166
reto Avatar answered Sep 24 '22 12:09

reto