Datetime parsing error

只谈情不闲聊 提交于 2019-11-28 06:05:57

问题


I am having problem parsing dates in Java. Below is the code.

 String dateString = "2017-12-13T16:49:20.730555904Z";
 List<String> formatStrings = Arrays.asList("yyyy-MM-dd'T'HH:mm:ss'Z'", "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'");

    for (String formatString : formatStrings)
    {
        try
        {
            SimpleDateFormat formatter = new SimpleDateFormat(formatString);
            formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
            Date d = formatter.parse(dateString);
            System.out.println("Format is:" + formatString);
            System.out.println("Orignal Date: " + d);
            System.out.println("Orignal MS: " + d.getTime());

            return d;
        }
        catch (ParseException e) {}
    }

    return null;
}

When I run this program, I get the following output.

Format is:yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'
Orignal Date: Fri Dec 22 03:45:15 UTC 2017
Orignal MS: 1513914315904

I am not sure why it is giving me Dec 22 when it should be Dec 13. But if I change my input date to this.

String dateString = "2017-12-13T16:49:20.7Z";

i.e only one character before the Z. then I get the correct output.

Format is:yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'
Orignal Date: Wed Dec 13 16:49:20 UTC 2017
Orignal MS: 1513183760007

It gives me correct output till 3 numbers before the Z. If more than 3 numbers than I got the wrong output.

It would be great if someone can point me out what I am missing here.

PS: I am parsing these dates in android. I have set min API level to 16 and java.time is not available for API level below 26.


回答1:


S in SimpleDateFormat specifies a number of milliseconds, not a fraction of a second. You've specified 730555904 milliseconds, which is ~8.45 days - hence the date change.

java.util.Date only has a precision of milliseconds. I would recommend using the java.time package, which has a precision of nanoseconds, like your input. Use DateTimeFormatter for parsing. Note that in DateTimeFormatter, the S specifier is for fraction-of-a-second.

Even better, Instant.parse uses the right format anyway:

import java.time.*;

public class Test {
    public static void main(String... args) throws Exception {
        String text = "2017-12-13T16:49:20.730555904Z";
        Instant instant = Instant.parse(text);
        System.out.println(instant);
    }
}


来源:https://stackoverflow.com/questions/47798849/datetime-parsing-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!