SimpleDateFormat problems with 2 year date

John Farrelly

I'm trying to understand two things:

  1. Why doesn't the following code throw an exception (since the SimpleDateFormat is not lenient)
  2. It doesn't throw an exception, but why is it parsing the year as 0013 (instead of using the rules here the +80:-20 years from today rule)

Here's the code

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TestDate {
    public static void main(String[] args) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
        format.setLenient(false);
        Date date = format.parse("01/01/13"); // Since this only has a 2 digit year, I would expect an exception to be thrown

        System.out.println(date); // Prints Sun Jan 01 00:00:00 GMT 13

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        System.out.println(cal.get(Calendar.YEAR)); // Prints 13
    }
}

If it makes a difference, I'm using java 1.6.0_38-b05 on Ubuntu

Evgeniy Dorofeev

SimpleDateFormat API:

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.

As for lenient, when it's set to false parse throws exception for invalid dates, eg 01/32/12, while in lenient mode this date is treated as 02/01/12. SimpleDateFormat uses Calendar internally, details about leniency can be found in Calendar API.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java SimpleDateFormat shifts Date by one year

From Dev

Android: SimpleDateFormat changes year of my date

From Java

SimpleDateFormat week year

From Dev

SimpleDateFormat provide wrong year

From Dev

Two-digit year in SimpleDateFormat

From Dev

Two-digit year in SimpleDateFormat

From Dev

Yii2 GridView filter date by year

From Dev

Misbehaving of the date parsing in SimpleDateFormat

From Dev

Unparseable Greek date - SimpleDateFormat

From Dev

Another Unparseable date with SimpleDateFormat

From Dev

Unparseable date with timezone and SimpleDateFormat

From Dev

Unparsable Date exception with SimpleDateFormat

From Dev

Java unparsable date SimpleDateFormat

From Dev

Simpledateformat unparseable date

From Dev

SimpleDateFormat parsing wrong date

From Dev

Unparseable Date with SimpleDateFormat

From Dev

SimpleDateFormat date and time pattern

From Dev

SimpleDateFormat is parsing to a different Date

From Dev

Date difference using SimpleDateFormat

From Dev

SimpleDateFormat ParseException: Unparseable date

From Dev

Incorrect date parsing with SimpleDateFormat

From Dev

Get day, month and year separately using SimpleDateFormat

From Dev

SimpleDateFormat("dd-MMM-YYYY") printing year one year ahead

From Dev

how to format date using SimpleDateFormat

From Dev

SimpleDateFormat can not parse string to Date

From Dev

Postgresql Date type and java SimpleDateFormat

From Dev

SimpleDateFormat not returning uppercase formatted date

From Dev

Java SimpleDateFormat Pattern for JavaScript Date

From Dev

Incorrect parsing of date strings in SimpleDateFormat