DateTimeParseException:无法解析文本'2019-06-07 12:18:16' [英] DateTimeParseException: Text '2019-06-07 12:18:16' could not be parsed

查看:1001
本文介绍了DateTimeParseException:无法解析文本'2019-06-07 12:18:16'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码将Instant转换为String,然后将其转换回I

I have following code to convert an Instant to String then convert it back to I

String timestampString = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"));
LOGGER.info("timestampString: " + timestampString);

Instant instant =
    LocalDateTime.parse(timestampString,
        DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss")).toInstant(ZoneOffset.UTC);

将timestampString打印为:2019-06-07 12:45:57

it print the timestampString as: 2019-06-07 12:45:57

并且无法解析字符串:

java.time.format.DateTimeParseException: Text '2019-06-07 12:45:57' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {MinuteOfHour=45, HourOfAmPm=0, NanoOfSecond=0, SecondOfMinute=57, MilliOfSecond=0, MicroOfSecond=0},ISO resolved to 2019-06-07 of type java.time.format.Parsed

为什么即使我将时间戳转换成相同的格式也无法解析它?

why it cannot parse it even though that's the same format I convert the timestamp to?

推荐答案

在一天中的小时内使用HH代替hh

您要问的问题是(两次)在格式模式字符串中使用小写的hh.您需要从00到23的小时中的大写HH为小时.hh是从01到12的AM或PM中的小时.因此,出错的是java.time不知道字符串中的12是否引用了到凌晨12点或中午12点,并拒绝为您猜测.

Use HH for hour of day instead of hh

The problem that you are asking about is that you are using lowercase hh in your format pattern string (both times). You need uppercase HH for hour day from 00 through 23. hh is for hour within AM or PM from 01 through 12. So what went wrong was that java.time didn’t know whether 12 in your string referred to 12 AM or 12 PM and refused to make a guess for you.

如果您仔细阅读异常消息,您还将注意到它指出已解析HourOfAmPm=0.它没有说HourOfDay.

If you read the exception message closely you will also notice that it says that HourOfAmPm=0 was parsed. It doesn’t say HourOfDay.

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    String timestampString = LocalDateTime.now().format(formatter);
    System.out.println("timestampString: " + timestampString);

    Instant instant = LocalDateTime.parse(timestampString, formatter)
            .toInstant(ZoneOffset.UTC);

    System.out.println("instant: " + instant);

当我刚刚运行此代码段时,我得到了以下输出:

When I ran this snippet just now, I got this output:

timestampString: 2019-06-08 19:22:51
instant: 2019-06-08T19:22:51Z

这是错误!我在世界标准时间17:22而非19:22左右运行了代码段.由于丹麦仍在使用夏令时(该死),因此此处的当地时间为19:22,用于显示结果,并转换为UTC的同一挂钟时间,而不是同一时刻.您应该始终将所需的时区传递给now方法,以避免此类错误.由于您需要UTC:

This is wrong! I ran the snippet around 17:22 UTC, not 19:22. Since Denmark is still using summer time (damn), the local time here was 19:22, which was used for the result and translated into the same wall clock time in UTC, not the same instant. You should always pass your desired time zone to the now method to avoid such bugs. Since you wanted UTC:

    String timestampString = LocalDateTime.now(ZoneOffset.UTC).format(formatter);

timestampString: 2019-06-08 17:27:57
instant: 2019-06-08T17:27:57Z

更好的是,不要使用LocalDateTime暂时保存要使用的内容.改用InstantOffsetDateTimeZonedDateTime.

Still better, don’t use LocalDateTime for holding something that you want to use as a moment in time. Use Instant, OffsetDateTime or ZonedDateTime instead.

有关使用hhHHkk格式化和解析小时值的更多信息

There is more information on using hh, HH or kk for formatting and parsing hour values in this question and its answers: Difference between java HH:mm and hh:mm on SimpleDateFormat. The question is asking about the notoriously troublesome SimpleDateFormat, but the answers are valid for DateTimeFormatter too.

这篇关于DateTimeParseException:无法解析文本'2019-06-07 12:18:16'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆