java 获取yyyymmdd,如何在Java 8中解析字符串YYYYMMDD_HHMMSSZ

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值