My Oracle Support Banner

SimpleDateFormat Throws Java.text.ParseException for a Valid Date String (Doc ID 2040704.1)

Last updated on MAY 26, 2018

Applies to:

Java SE JDK and JRE - Version 7 and later
Information in this document applies to any platform.

Goal

SimpleDateFormat throws java.text.ParseException for a valid date string:  'SimpleDateFormat.parse() method Not able to parse the date string "3/8/2015 2:19:54 AM" in the time zone "(UTC-05:00) Eastern Time (US & Canada)".'


Sample code to reproduce the issue.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.TimeZone;

/**
 * Created by Thakkn on 7/10/2015.
 */
public class TimeTest
{
    public static void main (String[] args)
    {
        try
        {
            String date = "3/8/2015 2:19:54 AM";
            SimpleDateFormat dateFormat = new SimpleDateFormat("M/d/yyyy h:mm:ss a",Locale.getDefault());
            dateFormat.setTimeZone(TimeZone.getDefault());
            dateFormat.setLenient(false);
            System.out.println(dateFormat.parse(date));
        }
        catch (ParseException e)
        {
            e.printStackTrace();
        }
    }

}

  
You need to set the time zone to "(UTC-05:00) Eastern Time (US & Canada)" before running the test code.

You'll see the same using US/Pacific TZ :  

$TZ="US/Pacific"

/java/re/jdk/7u85/latest/binaries/solaris-sparc/bin/java -showversion

TimeTest
java version "1.7.0_85"
Java(TM) SE Runtime Environment (build 1.7.0_85-b15)
Java HotSpot(TM) Server VM (build 24.85-b06, mixed mode)

java.text.ParseException: Unparseable date: "3/8/2015 2:19:54 AM"
        at java.text.DateFormat.parse(DateFormat.java:357)
        at TimeTest.main(TimeTest.java:19)

  

Error With JDK-8u51:

java.text.ParseException: Unparseable date: "3/8/2015 2:19:54 AM"
at java.text.DateFormat.parse(DateFormat.java:366)
at TimeTest.main(TimeTest.java:19)

 

Solution

To view full details, sign in with your My Oracle Support account.

Don't have a My Oracle Support account? Click to get started!


In this Document
Goal
Solution
References


My Oracle Support provides customers with access to over a million knowledge articles and a vibrant support community of peers and Oracle experts.