deb
deb

Reputation: 425

Error using SimpleDateFormat

I'm trying to use the SimpleDateFormat class to parse a DateTime out of this string:

05-Jul-2012 11:38:02,442 UTC AM

I tried the following format string:

SimpleDateFormat dateformatYYYYMMDD = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss,SSS z a");

Date temp = dateformatYYYYMMDD.parse(time);

But it generates the error:

Error: Unparseable date: ""

If I use zZ I get : Error: Unparseable date: "05-Jul-2012 11:38:02,442 UTC AM"

Any hints on how to get around this?

Upvotes: 3

Views: 963

Answers (5)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79075

As Jon Skeet has pointed out, it is a Locale-related issue. However, he missed an important point. Since your date-time string has an AM/PM marker, it does not make sense to use H in the pattern. You should have used h instead of H. The symbol, H is used for hour-of-day (0-23) while h is used for am-pm-of-day. Check the documentation pages [1][2] to learn more about it.

How is it a Locale-related issue?

By default, the date-time parsing/formatting API uses the default Locale (i.e. the Locale set in your JVM) when you do not specify a Locale. Since your date-time string is in English, you should use an English-Locale; otherwise, your parsing/formatting code will work only when executed on a JVM with an English-Locale as the default. Check this answer to learn more about it.

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.

Solution using modern date-time API

Even in English-speaking countries, am/pm markers may be written in different cases e.g. Locale.ENGLISH, Locale.US and Locale.ROOT accept AM/PM but Locale.UK accepts am/pm.

So, while you can use DateTimeFormatter.ofPattern("dd-MMM-uuuu hh:mm:ss,SSS z a", Locale.ENGLISH) to parse your date-time string, I recommend you build and use a case-insensitive DateTimeFormatter as shown in the demo.

Demo:

class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtf = new DateTimeFormatterBuilder()
                .parseCaseInsensitive()
                .appendPattern("dd-MMM-uuuu hh:mm:ss,SSS z a")
                .toFormatter(Locale.ENGLISH);

        ZonedDateTime zdt = ZonedDateTime.parse("05-Jul-2012 11:38:02,442 UTC AM", dtf);
        System.out.println(zdt);
    }
}

Output:

2012-07-05T11:38:02.442Z[Etc/UTC]

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 your date-time string and convert zdt from the above code into a java.util.Date instance using Date date = Date.from(zdt.toInstant()).

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

Upvotes: 1

Miquel
Miquel

Reputation: 15675

I'm not able to reproduce your error. Are you sure you are using:

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

Upvotes: 0

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

Try this...

System.out.println(new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss,SSS z a",
                                              Locale.US).format(new Date()));

Upvotes: 0

Jon Lin
Jon Lin

Reputation: 143886

That works fine for me when I try to run:

String time = "05-Jul-2012 11:38:02,442 UTC AM";
SimpleDateFormat dateformatYYYYMMDD = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss,SSS z a");
Date temp = dateformatYYYYMMDD.parse(time);

Are you sure time is set to something?

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500525

The code you've given works fine for me. Perhaps the problem is your locale? Try specifying it when you create the SimpleDateFormat:

SimpleDateFormat format =new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss,SSS z a",
                                              Locale.US);

That way it will try to find US month and am/pm designator names.

Upvotes: 6

Related Questions