instant java,关于java:Format Instant to String
我正在嘗試使用新的java 8 time-api和模式將Instant格式化為String:
Instant instant = ...;
String out = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(instant);
使用上面的代碼我得到一個(gè)異常,它抱怨一個(gè)不受支持的字段:
java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: YearOfEra
at java.time.Instant.getLong(Instant.java:608)
at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298)
...
時(shí)區(qū)
要格式化Instant,需要時(shí)區(qū)。如果沒有時(shí)區(qū),格式化程序?qū)⒉恢廊绾螌⒓磿r(shí)字段轉(zhuǎn)換為人類日期時(shí)間字段,因此會(huì)拋出異常。
可以使用withZone()將時(shí)區(qū)直接添加到格式化程序中。
DateTimeFormatter formatter =
DateTimeFormatter.ofLocalizedDateTime( FormatStyle.SHORT )
.withLocale( Locale.UK )
.withZone( ZoneId.systemDefault() );
生成字符串
現(xiàn)在使用該格式化程序生成Instant的String表示形式。
Instant instant = Instant.now();
String output = formatter.format( instant );
轉(zhuǎn)儲(chǔ)到控制臺(tái)。
System.out.println("formatter:" + formatter +" with zone:" + formatter.getZone() +" and Locale:" + formatter.getLocale() );
System.out.println("instant:" + instant );
System.out.println("output:" + output );
跑步時(shí)
formatter: Localized(SHORT,SHORT) with zone: US/Pacific and Locale: en_GB
instant: 2015-06-02T21:34:33.616Z
output: 02/06/15 14:34
謝謝!! BTW例外的"不支持的字段"指向例如年,是非常鈍的。也許應(yīng)該檢測到這種情況,并且應(yīng)該拋出直接指向Instant中缺少的區(qū)域id的異常!
更奇怪的是,如果你包括.withZone(例如.withZone(ZoneId.of("Z")))并格式化LocalDateTime,那么該區(qū)域就是IGNORED!因此,只要包含.withZone(),就可以對Instant和LocalDateTime使用相同的格式化程序,而不會(huì)影響后者顯示的時(shí)間。
為什么接受Instant有一個(gè)GMT的TimeZone是如此困難?
public static void main(String[] args) {
DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
.withZone(ZoneId.systemDefault());
System.out.println(DATE_TIME_FORMATTER.format(new Date().toInstant()));
}
雖然此代碼可能會(huì)回答這個(gè)問題,但提供有關(guān)如何以及為何解決問題的其他背景將提高答案的長期價(jià)值。
雖然此代碼段可以解決問題,但包括解釋確實(shí)有助于提高帖子的質(zhì)量。請記住,您將來會(huì)回答讀者的問題,而這些人可能不知道您的代碼建議的原因。
Instant類不包含區(qū)域信息,它僅存儲(chǔ)UNIX紀(jì)元的時(shí)間戳(以毫秒為單位),即UTC的1月1日1070。
因此,格式化程序無法打印日期,因?yàn)槿掌谑冀K打印為具體時(shí)區(qū)。
您應(yīng)該將時(shí)區(qū)設(shè)置為格式化程序,一切都會(huì)好的,如下所示:
Instant instant = Instant.ofEpochMilli(92554380000L);
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withLocale(Locale.UK).withZone(ZoneOffset.UTC);
assert formatter.format(instant).equals("07/12/72 05:33");
assert instant.toString().equals("1972-12-07T05:33:00Z");
DateTimeFormatter.ISO_INSTANT.format(Instant.now())
這樣您就不必轉(zhuǎn)換為UTC。但是,其他一些語言的時(shí)間框架可能不支持毫秒,所以你應(yīng)該這樣做
DateTimeFormatter.ISO_INSTANT.format(Instant.now().truncatedTo(ChronoUnit.SECONDS))
你是什??么意思"其他語言的時(shí)間框架"? ISO_INSTANT.format()會(huì)自動(dòng)截?cái)酁槊雴?#xff1f;
有些框架只希望時(shí)間達(dá)到秒,因?yàn)樗鼈儾蛔裱暾腎SO約定,并且當(dāng)輸入字符串的一部分有毫秒時(shí)就會(huì)中斷。
或者,如果您仍想使用從模式創(chuàng)建的格式化程序
你可以使用LocalDateTime而不是Instant:
LocalDateTime datetime = LocalDateTime.now();
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(datetime)
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd");
String text = date.toString(formatter);
LocalDate date = LocalDate.parse(text, formatter);
我相信這可能會(huì)有所幫助,您可能需要使用某種localdate變體而不是即時(shí)變體
總結(jié)
以上是生活随笔為你收集整理的instant java,关于java:Format Instant to String的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: YOLOv5损失函数定义
- 下一篇: LNK2019 无法解析的外部符号 __