3

I am getting issues with Date Format. I am using SimpleDateFormat to parse date and using MM/dd/yyyy format to parse date, its working properly with correct input like 11/11/2011 and its returning correct result (Fri Nov 11 00:00:00 IST 2011)

But if we enter 11/11/11 as input then its not working properly (Wed Nov 11 00:00:00 IST 11), nor giving parse error.

public static void main(String[] args) {
    String format = "MM/dd/yyyy";
    String dt = "11/11/11";
    Date date = null;
    try {
        date = TestDate.parDate(dt, format);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    System.out.println(date);

}

public static Date parDate(String value, String dateFormat) throws ParseException {
    Date date = null;
    date = new SimpleDateFormat(dateFormat).parse(value);
    return date;
}
Kuldeep
  • 599
  • 11
  • 28
  • 3
    you explicitly write that the year need to be on 4 digits (`yyyy`) – jhamon Aug 03 '18 at 07:24
  • Possible duplicate of [Java 8 DateTimeFormatter two digit year 18 parsed to 0018 instead of 2018?](https://stackoverflow.com/questions/48156022/java-8-datetimeformatter-two-digit-year-18-parsed-to-0018-instead-of-2018) – Ole V.V. Aug 04 '18 at 15:53
  • I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Aug 04 '18 at 15:53

4 Answers4

3

If you can use Java 8 or above, use DateTimeFormatter and LocalDate classes to parse date values. It will throw an error if the input is not in the expected format.

DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("MM-dd-yyyy");
LocalDate date1 = LocalDate.parse("11-11-2011", formatter1);
System.out.println(date1);

Above will work as expected but if you try to parse "11-11-11" with the same formatter object you will get an exception like

Exception in thread "main" java.time.format.DateTimeParseException: Text '11-11-11' could not be parsed at index 6
at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.LocalDate.parse(Unknown Source)
at com.is.TestDate.main(TestDate.java:14)
maneesh
  • 1,092
  • 1
  • 8
  • 11
2

Use MM/dd/yy for both 11/11/11 and 11/11/2011.

zhh
  • 2,346
  • 1
  • 11
  • 22
  • 1
    Just a remark, but you have to pay attention that when using the MM/dd/yy format: you can have possible mistakes when using dates in different centuries. For example 01/01/20, do you mean 1920 or 2020? – Mathias G. Aug 03 '18 at 07:43
  • @Kuldeep You're welcome. If you think this answer solves your problem, check it as the correct answer. – zhh Aug 05 '18 at 17:33
1

If you want to put a strict check, that would not be possible with SimpleDateFormat. You can instead put a pattern check on the date string:

if (!dt.matches("\\d{2}/\\d{2}/\\d{4}")) {
    //throw exception
}
S.K.
  • 3,597
  • 2
  • 16
  • 31
1

It's not an issue. It's correct accroding to Java API of SimpleDateFormat. It says regarding the pattern of Year:

For parsing, if the number of pattern letters is more than 2, the year is interpreted literally, regardless of the number of digits. So using the pattern "MM/dd/yyyy", "01/11/12" parses to Jan 11, 12 A.D.

Thus there is no exception thrown as you provided a valid input.

If you have to deal with two different date formats for a year yyyy and yy convert one version to the other or use two formatter - if you have to use SimpleDateFormat at all.

That's the way since Java 8: How to parse/format dates with LocalDateTime? (Java 8)

LuCio
  • 5,055
  • 2
  • 18
  • 34