2

//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

11
  • 1
    1990 -- yyyy 04--MM 16--dd 00--HH 00--mm 00--ss Commented May 18, 2017 at 10:03
  • 2
    I did compile with your code. No any error Commented May 18, 2017 at 10:03
  • 2
    Same as kimdung. Ran code. No error. Unable to reproduce. Commented May 18, 2017 at 10:04
  • 2
    there we go ideone.com/tRrZRk Commented May 18, 2017 at 10:04
  • Code works fine for me, maybe you have wrong imports? Try cleaning your project. Commented May 18, 2017 at 10:09

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Brilliant, The People's Republic of China experimented with DST from 1986, but abandoned DST from 1992 onwards. The PRC now uses one time zone (UTC+8) for the whole country. timeanddate.com/time/change/china/beijing?year=1990
Thank you very much ! the problem has bothered me for a long time
1

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.

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.