Converting String into Date

suggest change

DateFormat.parse converts a date string into a Date. It throws the checked ParseException. Date.getYear() is deprecated and returns the year minus 1900, so 2016 is printed as 116. Prefer java.time for new code.

DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
String dateStr = "02/25/2016"; // input String
Date date = dateFormat.parse(dateStr);
System.out.println(date.getYear()); // 116, meaning 2016

The US short style is typically M/d/yy. This parser is lenient, so a four-digit year in that field can still be accepted. There are four styles, SHORT, MEDIUM (the default), LONG, and FULL. They depend on the locale. If no locale is specified, the system default locale is used. These are formats for 30 June 2009:

Style Locale.US Locale.FRANCE
SHORT 6/30/09 30/06/09
MEDIUM Jun 30, 2009 30 juin 2009
LONG June 30, 2009 30 juin 2009
FULL Tuesday, June 30, 2009 mardi 30 juin 2009

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents