繁体   English   中英

将Java 8 ISO 8601持续时间表达式添加到java.util.Date

[英]Add a Java 8 ISO 8601 time duration expression to java.util.Date

我有一个像"PT20.345S""P2DT3H4M"等表达式,如https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html#parse-java.lang所述。 CharSequence-

我如何解析它,将它添加到当前时间并获取java.util.Date对象?

两者都不起作用:

Date d1 = Date.from(LocalDateTime.now().plus(Duration.parse(_expression)));
Date d2 = Date.from(Duration.parse(_expression).addTo(LocalDateTime.now()));
Duration amountToAdd = Duration.parse("PT20.345S");  // Represent a span of time. Here, about twenty and a third seconds.
Instant now = Instant.now() ;                        // Capture the current moment in UTC.
Instant otherMoment = now.plus(amountToAdd);         // Add the span-of-time to the current moment, for a moment in the future (or in the past if the duration is negative).
String output = otherMoment.toString():              // Generate a String in standard ISO 8601 format.

2018-06-30T19:34:47Z

从现代java.time类转换为遗留类。

Date date1 = Date.from(otherMoment);
System.out.println(date1);

刚刚在欧洲/哥本哈根时区运行我得到:

6月30日星期六21:34:47 CEST 2018

如果我使用你的另一个示例持续时间字符串P2DT3H4M ,我得到:

星期二03月03日00:38:26 CEST 2018

或者,如果你是一个单行:

    Date date1 = Date.from(Instant.now().plus(Duration.parse("PT20.345S")));

java.util.Date类很久了,所以理想情况下你不应该有一个。 如果您还需要一个,通常是对于您无法更改或不想立即更改的旧API,您在使用java.time (现代Java日期和时间API)尽可能多地执行逻辑时正在思考,最后只转换为Date 现代世界中Date最亲近的堂兄是Instant ,并且InstantDate之间存在直接转换,这就是我使用这个类的原因。 Instant也很可爱,不受区域偏移和时区的影响。

在您的代码中,如果您使用ZoneOffsetLocalDateTime转换为Instant (示例UTC,或使用ZoneOffset.systemDefault()系统默认值),第一个解决方案应该可以正常工作,如下所示:

Date d1 = Date.from(LocalDateTime.now().plus(Duration.parse(_expression)).toInstant(OffsetDateTime.now().getOffset());

但是 ,在这种情况下, LocalDateTime被错误地使用,因为它不代表片刻,不是时间轴上的一个点

来自javadoc

此类不存储或表示时区。 相反,它是用于生日的日期的描述,结合在挂钟上看到的当地时间。 如果没有附加信息(如偏移或时区),它不能代表时间线上的瞬间。

但是, Instant是UTC时间轴上的一个时刻

该类在时间线上模拟单个瞬时点。 这可能用于在应用程序中记录事件时间戳。

因此,如果您使用Instant,则无论时区如何,您都可以确切地知道正在引用的时刻。 由于您将处理业务逻辑,例如将时间量添加到当前时间并转换为Date,因此这是一个方便使用的类。

 Date date1 = Date.from(Instant.now().plus(Duration.parse("PT20.345S")));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM