Bowie
Bowie

Reputation: 41

java simpledateformat parse string error

//there is my codes

 public static void main(String[] args) {

    String a = "19900416000000";
    String b = "19900415000000";
    DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
    df.setLenient(false);

    try {
        df.parse(a);
    } catch (ParseException e) {
        System.out.println("a parse error");
        e.printStackTrace();
    }

    try {
        df.parse(b);
    }catch (ParseException e){
        System.out.println("b parse error");
        e.printStackTrace();
    }

// and I get an error,like this

b parse error
java.text.ParseException: Unparseable date: "19900415000000"
    at java.text.DateFormat.parse(DateFormat.java:366)
    at org.suanhua.elasticsearch.client.ETLTest.main(ETLTest.java:179)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

So,what's the different between a and b? why parse(b) get an error?

//there is my import

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

// use jdk8 under intellij

Upvotes: 2

Views: 1194

Answers (2)

Anonymous
Anonymous

Reputation: 86389

Very well spotted, Andreas.

FWIW, Bowie, the newer date and time classes in your JDK 8 can parse the strings and give you for example

1990-04-16T00:00+09:00[Asia/Chongqing]
1990-04-15T01:00+09:00[Asia/Chongqing]

You will notice the second string has 01 for hours where it said 00 in the input string. I used like (edit: added creation of dtf):

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        System.out.println(LocalDateTime.parse(b, dtf)
                               .atZone(ZoneId.of("Asia/Chongqing")));

If you just need the computer’s default time zone, you may use ZoneId.systemDefault().

In any case, since you are using JDK 8, is there any good reason why you are sticking with the outdated classes SimpleDateFormat and friends? The newer classes are generally much nicer to work with.

If you need to catch the date-time falling in the summer time gap, you will have to handle it specially, though. For example convert back to a LocalDateTime and see if you get the same as the one you parsed.

PS I still have an observation I cannot explain satisfactorily: One of the time zones Andreas mentions is CTT. I take this to mean China Taiwan Time, so I had expected it to be the same as Asia/Taipei. However, when I put the latter into my code, I get 1990-04-15T00:00+08:00[Asia/Taipei], that is, 00 hours, so here the time 0:00 midnight exists. For the other 5 time zones Andreas mentions I get 01 hours.

Upvotes: 1

Andreas
Andreas

Reputation: 159250

Your problem seems to be timezone related.

Testing all timezones in my jdk1.8.0_91 installation.

String a = "19900416000000";
String b = "19900415000000";
DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
df.setLenient(false);

for (String id : TimeZone.getAvailableIDs()) {
    df.setTimeZone(TimeZone.getTimeZone(id));
    try {
        df.parse(a);
    } catch (ParseException e) {
        System.out.println(id + ": " + e);
    }
    try {
        df.parse(b);
    } catch (ParseException e) {
        System.out.println(id + ": " + e);
    }
}

Output

Asia/Chongqing: java.text.ParseException: Unparseable date: "19900415000000"
Asia/Chungking: java.text.ParseException: Unparseable date: "19900415000000"
Asia/Harbin: java.text.ParseException: Unparseable date: "19900415000000"
Asia/Shanghai: java.text.ParseException: Unparseable date: "19900415000000"
PRC: java.text.ParseException: Unparseable date: "19900415000000"
CTT: java.text.ParseException: Unparseable date: "19900415000000"

If your default timezone is any of those, then it fails because midnight of April 15, 1990 doesn't exist, as that was the time when Daylight Savings Time started, i.e. at midnight, the clock was set forward to 1 AM, so midnight didn't exist.

Upvotes: 5

Related Questions