8

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();
        }
    }

}

3 Answers 3

8

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");
Sign up to request clarification or add additional context in comments.

4 Comments

you used a different String , but the actual String is "2011-11-19T00:00:00.000-05:00"; , I want the colon also
was just the explanation why it fails, and one way how to work around it.
I would investigate why you get a date, which is not RFC conform.. you can workaround it by just removing the last colon, but this is obviously a bad work around.
Here's how I removed the colon: String dateStr = "2013-06-25T16:09:41.276-05:00"; Matcher m = Pattern.compile("(\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}-)(\\d{2}:\\d{2})").matcher(dateStr); if (m.matches()) { dateStr = m.group(1) + m.group(2).replace(":", ""); } Date date = new SimpleDateFormat("yyyy-M-d'T'HH:mm:ss.SSSZ").parse(dateStr); System.out.printf("Date = '%s'", date);
5

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.)

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.