如何在Java 8中解析字符串YYYYMMDD_HHMMSSZ [英] How to parse a String YYYYMMDD_HHMMSSZ in Java 8

查看:816
本文介绍了如何在Java 8中解析字符串YYYYMMDD_HHMMSSZ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析UTC日期和时间字符串,例如20180531_132001Z成为Java 8的日期和时间对象。如何使用Java 8的新日期和时间库进行此操作?我看到的大多数示例都是LocalDateTime,如下所示:

  DateTimeFormatter formatter = DateTimeFormatter.ofPattern(yyyyMMdd_HHmmss'Z'); 
LocalDateTime localDateTime = LocalDateTime.parse(20180531_132001Z,formatter);
System.out.println(localDateTime);
System.out.println(localDateTime.atOffset(ZoneOffset.UTC));

代码输出:

  2018-05-31T13:20:01 
2018-05-31T13:20:01Z




  • 这是当地时间还是UTC时间?我正在解析的字符串值是基于UTC的,所以我想知道在持久化到数据库之前是否需要做进一步的操作。

  • 如果是前者,我该如何将其转换为UTC日期和时间?



我最终需要将其保存到SQL Server数据库表中(列类型为 [ datetime2](7),使用[Spring] JDBC。



更新:根据评论和答案,我认为我的问题没有经过深思熟虑。换句话说,如果我得到一个输入字符串并且我解析它而不考虑任何区域或偏移量,我将获得 LocalDateTime 对象。如何获取该对象并将封装后的值转换为UTC日期和时间?

解决方案

LocalDateTime 可能会产生误导。它不代表 您的 本地日期/时间,代表 a 本地日期/时间。
它根本没有时区信息。
T.帽子是,它只是说它是13:20。它没有说在哪里它是13:20。您可以自行解释 where 部分。



由于此 LocalDateTime 是对于携带时间戳通常不是很有用,它仅适用于时区依赖于某些上下文的情况。 1



使用时间戳时,它是最好使用 ZonedDateTime OffsetDateTime 。它们带有日期,时间和偏移量。



所以 localDateTime.atOffset(ZoneOffset.UTC)实际上会返回一个 OffsetDateTime 的实例,将 localDateTime 解释为UTC时间。



<有人可能会说你可以通过首先解析时区信息来避免解释部分(即使它总是 Z ):

  String example =20180531_132001Z; 
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(yyyyMMdd_HHmmssX);
OffsetDateTime dateTime = OffsetDateTime.parse(例如,formatter);
System.out.println(dateTime); //看看ma,没有硬编码的UTC

将打印:

  2018-05-31T13:20:01Z 

附加值是您的代码自动支持时区(例如20180531_132001 + 05)。



JDBC 4.2兼容的驱动程序可以通过调用 setObject



JDBC驱动程序可以将 dateTime 转换为 java.sql.Timestamp java.util。日期

  java.sql.Timestamp.from(dateTime.toInstant()); 
java.util.Date.from(dateTime.toInstant());






1 有几乎总是一些上下文,其中 LocalDateTime 运行。例如 Flight KL1302明天13:20到达机场X 明天13:20 的背景是机场X的当地时间;它可以通过查找X的时区来确定。


I need to parse a UTC date and time string, e.g. 20180531_132001Z into a Java 8 date and time object. How do I go about doing this using Java 8's new date and time libraries? Most examples I see is for LocalDateTime, like this:

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss'Z'");
    LocalDateTime localDateTime = LocalDateTime.parse("20180531_132001Z", formatter);
    System.out.println(localDateTime);
    System.out.println(localDateTime.atOffset(ZoneOffset.UTC));

The code outputs:

2018-05-31T13:20:01
2018-05-31T13:20:01Z

  • Is this considered local time or UTC time? The string value I am parsing is based on UTC, so I am wondering if I need to do anything further before persisting to the database.
  • If the former, how do I convert that to UTC date and time?

I ultimately need to persist this to a SQL Server database table (column type is [datetime2](7), using [Spring] JDBC.

Update: Based on the comments and answers, I think my question is not well thought out. Putting it another way, if I get an input string and I parse it without factoring any zone or offset, I will get a LocalDateTime object. How do I take that object and convert the encapsulated value to UTC date and time?

解决方案

LocalDateTime can be misleading. It doesn't represent your local date/time, it represents a local date/time. It carries no time zone info at all. That is, it just says for example "it's 13:20". It doesn't say where it's 13:20. It's up to you to interpret the where part.

Due to this LocalDateTime is usually not very useful for carrying timestamps, it's only useful for situations when the timezone is dependent on some context.1

When working with timestamps it's better to use ZonedDateTime or OffsetDateTime instead. These carry the date, time and offset.

So localDateTime.atOffset(ZoneOffset.UTC) will actually return an instance of OffsetDateTime, by interpreting localDateTime as UTC time.

One could argue that you can avoid the interpreting part by parsing with the timezone info in the first place (even though it's always Z):

    String example = "20180531_132001Z";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmmssX");
    OffsetDateTime dateTime = OffsetDateTime.parse(example, formatter);
    System.out.println(dateTime); // look ma, no hardcoded UTC

Will print:

2018-05-31T13:20:01Z

The added value is that your code automatically supports timezones (e.g. "20180531_132001+05").

JDBC 4.2 compliant driver may be able to directly address java.time types by calling setObject.

For older JDBC drivers you can convert dateTime to a java.sql.Timestamp or java.util.Date:

java.sql.Timestamp.from(dateTime.toInstant());
java.util.Date.from(dateTime.toInstant());


1 There is almost always some context in which LocalDateTime operates. For example "Flight KL1302 arrives at airport X tomorrow at 13:20". Here the context of "tomorrow at 13:20" is the local time at airport X; it can be determined by looking up the time zone of X.

这篇关于如何在Java 8中解析字符串YYYYMMDD_HHMMSSZ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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