簡體   English   中英

如何從TemporalAccessor獲取ZoneId,LocalDateTime和Instant

[英]How to get ZoneId, LocalDateTime and Instant from TemporalAccessor

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

    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();

當我通過調試檢查時, formatter.parse(time)已經包含了紀元時間和時區:

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

我的問題是如何從formatter.parse(time)提取時區(這里是澳大利亞/阿德萊德),該formatter.parse(time)Parsed類型,以便在此行的atZone()中動態使用時區?

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

即使是Furthur,如何從formatter.parse(time)響應中提取InstantSeconds=1559456477 ,以避免采取以下更多步驟來獲取紀元時間戳?

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

localDateTime.toInstant().toEpochMilli();

DateTimeFormatter.parse方法返回包含日期,時間和其他一些信息的TemporalAccessor ,您可以通過查詢獲取該信息

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

定義對臨時對象的只讀訪問的框架級接口,例如日期,時間,偏移或這些的某種組合。

這是日期,時間和偏移對象的基本接口類型。 它由那些可以提供信息作為字段或查詢的類實現。

TemporalQuery是功能界面

最常見的實現是方法引用,例如LocalDate :: from和ZoneId :: from。 其他常見查詢作為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);

您無法從DateTimeFormatter提取秒或區域ID,但您可以使用ZonedDateTime對象來執行此操作。 看看下面的代碼,並想知道你的時間String似乎在"America/Manaus" ,這似乎不等於澳大利亞中央標准時間 ......

public static void main(String args[]) throws Exception {
    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);

    // parse a time object using the formatter and the time String
    ZonedDateTime zdt = ZonedDateTime.parse(time, formatter);
    // print it using a standard formatter
    System.out.println(zdt.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
    // extract the zone id
    ZoneId zoneId = zdt.getZone();
    // print the zone id
    System.out.println("This time is of zone " + zoneId);

    // retrieve the instant seconds
    Instant instant = zdt.toInstant();
    // print the epoch seconds of another time zone
    System.out.println("Epoch seconds in Australia/Adelaide are " 
            + instant.atZone(ZoneId.of("Australia/Adelaide")).toEpochSecond());
}

當我運行它時,它輸出

2019-06-02T15:51:17-04:00[America/Manaus]
This time is of zone America/Manaus
Epoch seconds in Australia/Adelaide are 1559505077

它顯示了如何提取紀元秒和時區,但不知何故不承認ACST是它應該意味着什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM