Pawan
Pawan

Reputation: 32331

java.text.ParseException: Unparseable date:

Could anybody please tell me why i am getting java.text.ParseException: Unparseable date in the following code:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


public class Testdate {
    public static void main(String args[])
    {
        String text = "2011-11-19T00:00:00.000-05:00";
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        try {
            Date parsed = sdf.parse(text.trim());
            System.out.println(parsed);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

}

Upvotes: 8

Views: 38850

Answers (3)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79560

java.time

In March 2014, Java 8 introduced the modern, java.time date-time API which supplanted the error-prone legacy java.util date-time API. Any new code should use the java.time API*.

Also, shown below is a notice on the Joda-Time Home Page:

Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

Solution using modern date-time API

Given below is the excerpt from OffsetDateTime#parse documentation:

Obtains an instance of OffsetDateTime from a text string such as 2007-12-03T10:15:30+01:00.

The string must represent a valid date-time and is parsed using DateTimeFormatter.ISO_OFFSET_DATE_TIME.

Since your text string, 2011-11-19T00:00:00.000-05:00 fully complies with the default format, you do not need to specify any DateTimeFormatter explicitly.

Demo:

import java.time.*;

public class Main {
    public static void main(String[] args) {
        OffsetDateTime odt = OffsetDateTime.parse("2011-11-19T00:00:00.000-05:00");
        System.out.println(odt);
    }
}

Output:

2011-11-19T00:00-05:00

Online Demo

Note: If for some reason, you need an instance of java.util.Date, let java.time API do the heavy lifting of parsing the date-time string and convert odt from the above code into a java.util.Date instance using Date date = Date.from(odt.toInstant()).

Learn more about the modern Date-Time API from Trail: Date Time.


* If you are receiving an instance of java.util.Date, convert it tojava.time.Instant, using Date#toInstant and derive other date-time classes of java.time from it as per your requirement.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503290

Because the Z part of SimpleDateFormat's pattern support doesn't handle offsets with colons in.

I suggest you use Joda Time instead, using ISODateFormat.dateTime() to get an appropriate formatter.

(See this similar-but-not-quite-the-same-question from earlier today for more information.)

Upvotes: 5

Chris
Chris

Reputation: 8099

Its because of the colon in your timezone. Remove it and it will work:

String text = "2011-11-19T00:00:00.000-0500";
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

Upvotes: 8

Related Questions