簡體   English   中英

比較代表Java中時間戳的UTC的兩種不同字符串格式

[英]Comparing two different string formats for UTC that represents timestamp in java

我有一個字符串“ 21-SEP-15 02.48.48.000000000 AM UTC”,它表示utc時間戳,而我有“ 2015-10-08T20:13:21.3Z”格式的字符串,它表示另一個時間戳。 如果它們相等或不相等,如何比較它們?

第二種格式使用ISO約定,因此可以輕松對其進行解析。

第一個解析起來有些復雜,特別是因為月份是大寫的。

一種方法是:

String ts1 = "21-SEP-15 02.48.48.000000000 AM UTC";
String ts2 = "2015-10-08T20:13:21.3Z";

DateTimeFormatter fmt1 = new DateTimeFormatterBuilder()
        .parseCaseInsensitive()
        .appendPattern("dd-MMM-yy hh.mm.ss.SSSSSSSSS a VV")
        .toFormatter(Locale.ENGLISH);

Instant d1 = ZonedDateTime.parse(ts1, fmt1).toInstant();
Instant d2 = Instant.parse(ts2);

然后,您可以使用d1.equals(d2)比較兩個時刻。

暫無
暫無

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

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