24

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"));
Dave Cousineau
  • 12,154
  • 8
  • 64
  • 80
Jason
  • 303
  • 1
  • 5
  • 13
  • 1
    possible duplicate of [What is the best way to parse a date in MM/DD/YY format and adjust it to the current / previous century?](http://stackoverflow.com/questions/251535/what-is-the-best-way-to-parse-a-date-in-mm-dd-yy-format-and-adjust-it-to-the-cur) – Engineer2021 Jul 23 '14 at 10:31

4 Answers4

36

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.

reto
  • 9,995
  • 5
  • 53
  • 52
14

If I understand your question then yes,

SimpleDateFormat sdf = new SimpleDateFormat("yy/MM/dd", Locale.US);
Calendar cal = Calendar.getInstance(Locale.US);
cal.set(1900, 0, 1);
sdf.set2DigitYearStart(cal.getTime());
System.out.println(sdf.parse("16/07/23"));

Per the SimpleDateFormat.set2DigitYearStart(Date) javadoc,

Sets the 100-year period 2-digit years will be interpreted as being in to begin on the date the user specifies.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
7

There is the method set2DigitYearStart that can be used for that. It allows you to specify a start date. The parsed date will be in the interval [start date, start date + 100 years].

See the documentation for details.

Henry
  • 42,982
  • 7
  • 68
  • 84
1

May be you are looking for this.

    Calendar cal = Calendar.getInstance();
    cal.set( Calendar.YEAR, 1900 );
    SimpleDateFormat format = new SimpleDateFormat( "yy/MM/dd", Locale.US );
    format.set2DigitYearStart( cal.getTime() );
    System.out.println( format.parse( "16/07/23" ) );
Swaraj
  • 589
  • 4
  • 15
  • 1
    What do you need the Calendar for? – AlexS Jul 23 '14 at 06:38
  • setting year in dateformat format.set2DigitYearStart( cal.getTime() ); – Swaraj Jul 23 '14 at 06:51
  • 1
    You are not wrong to use Calendar but this answer needs some work. – Engineer2021 Jul 23 '14 at 10:32
  • what type of changes you are suggesting for. it will be good for me to keep in mind for future. – Swaraj Jul 23 '14 at 10:34
  • 2
    I would take out the "May be you are looking for this.". "May be" is one one not two. The answer should have sufficient explanation of what is going on. You put spaces between the parentheses, but typically that's a frowned upon style. If you are going to put a `System.out.println` then you should put another line that shows the example output. If you are able to host the a working example on an online code compiler site like ideone, that would be very helpful. All of this is to make your answer stand out and ensure it doesn't get downvoted. http://stackoverflow.com/help/how-to-answer – Engineer2021 Jul 23 '14 at 11:14