如何从 TemporalAccessor 获取 ZoneId、LocalDateTime 和 Instant [英] How to get ZoneId, LocalDateTime and Instant from TemporalAccessor

查看:145
本文介绍了如何从 TemporalAccessor 获取 ZoneId、LocalDateTime 和 Instant的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码来解析日期字符串:

 String time = "2 Jun 2019 03:51:17 PM ACST";字符串模式 = "d MMM yyyy hh:mm:ss a z";//z 检测时区(此处为 ACST)DateTimeFormatter 格式化程序 = DateTimeFormatter.ofPattern(pattern);//ZonedDateTime localDateTime = LocalDateTime.from(formatter.parse(time)).atZone(ZoneId.of("Australia/Adelaide"));//localDateTime.toInstant().toEpochMilli();

当我通过调试检查时,formatter.parse(time) 已经有纪元时间和时区:

//formatter.parse(time) :{InstantSeconds=1559456477},ISO,Australia/Adelaide 解析为 2019-06-02T15:51:17

我的问题是如何从 formatter.parse(time) 中提取时区(这里是澳大利亚/阿德莱德),它是 Parsed 类型以动态使用时区atZone() 在这一行?:

ZonedDateTime localDateTime = LocalDateTime.from(formatter.parse(time)).atZone(ZoneId.of("澳大利亚/阿德莱德"));

Even Furthur,如何从 formatter.parse(time) 响应中提取 InstantSeconds=1559456477 以避免采取以下更多步骤来获取纪元时间戳?:

ZonedDateTime localDateTime = LocalDateTime.from(formatter.parse(time)).atZone(ZoneId.of("澳大利亚/阿德莱德"));localDateTime.toInstant().toEpochMilli();

解决方案

DateTimeFormatter.parse 方法返回 TemporalAccessor 包含日期、时间和其他一些信息,您可以通过查询获取这些信息

默认 R 查询(TemporalQuery<R> 查询)

<块引用>

定义对时间对象的只读访问的框架级接口,例如日期、时间、偏移量或这些的组合.

这是日期、时间和偏移量对象的基本接口类型.它由那些可以提供字段或查询形式的信息的类实现.

TemporalQuery 功能正常界面

<块引用>

最常见的实现是方法引用,例如 LocalDate::from 和 ZoneId::from.TemporalQueries 中以静态方法的形式提供了其他常见查询.

String time = "2 Jun 2019 03:51:17 PM ACST";字符串模式 = "d MMM yyyy hh:mm:ss a z";DateTimeFormatter 格式化程序 = DateTimeFormatter.ofPattern(pattern);TemporalAccessor parsedTime = formatter.parse(time);ZoneId zone = parsedTime.query(ZoneId::from);LocalDateTime localDateTime = parsedTime.query(LocalDateTime::from);Instant Instant = parsedTime.query(Instant::from);System.out.println(zone);System.out.println(localDateTime);System.out.println(即时);

I'm using the below code to parse a date string:

    String time = "2 Jun 2019 03:51:17 PM ACST";
    String pattern = "d MMM yyyy hh:mm:ss a z"; // z detects the time zone (ACST here)
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);

 // ZonedDateTime localDateTime = LocalDateTime.from(formatter.parse(time)).atZone(ZoneId.of("Australia/Adelaide"));
 // localDateTime.toInstant().toEpochMilli();

The formatter.parse(time) already has both epoch time and time zone when I inspect it through debugging:

// formatter.parse(time) :
{InstantSeconds=1559456477},ISO,Australia/Adelaide resolved to 2019-06-02T15:51:17

My question is how to extract time zone (here Australia/Adelaide) from formatter.parse(time) which is of Parsed type to use the time zone dynamically in atZone() at this line? :

ZonedDateTime localDateTime = LocalDateTime.from(formatter.parse(time)).
                                     atZone(ZoneId.of("Australia/Adelaide"));

Even Furthur, how to extract the InstantSeconds=1559456477 from the formatter.parse(time) response to avoid taking the more steps below to get epoch timestamp? :

ZonedDateTime localDateTime = LocalDateTime.from(formatter.parse(time)).
                                     atZone(ZoneId.of("Australia/Adelaide"));

localDateTime.toInstant().toEpochMilli();

解决方案

The DateTimeFormatter.parse method return the TemporalAccessor which contains date, time and some other information and you can get that information by querying

default <R> R query(TemporalQuery<R> query)

Framework-level interface defining read-only access to a temporal object, such as a date, time, offset or some combination of these.

This is the base interface type for date, time and offset objects. It is implemented by those classes that can provide information as fields or queries.

TemporalQuery is functional interface

The most common implementations are method references, such as LocalDate::from and ZoneId::from. Additional common queries are provided as static methods in TemporalQueries.

String time = "2 Jun 2019 03:51:17 PM ACST";
String pattern = "d MMM yyyy hh:mm:ss a z";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);

TemporalAccessor parsedTime = formatter.parse(time);

ZoneId zone = parsedTime.query(ZoneId::from);

LocalDateTime localDateTime = parsedTime.query(LocalDateTime::from);

Instant instant = parsedTime.query(Instant::from);

System.out.println(zone);
System.out.println(localDateTime);
System.out.println(instant);

这篇关于如何从 TemporalAccessor 获取 ZoneId、LocalDateTime 和 Instant的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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