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);
使用上面的代碼我得到一個異常,它抱怨一個不受支持的字段:
java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: YearOfEra
at java.time.Instant.getLong(Instant.java:608)
at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298)
...
時區(qū)
要格式化Instant,需要時區(qū)。如果沒有時區(qū),格式化程序?qū)⒉恢廊绾螌⒓磿r字段轉(zhuǎn)換為人類日期時間字段,因此會拋出異常。
可以使用withZone()將時區(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)儲到控制臺。
System.out.println("formatter:" + formatter +" with zone:" + formatter.getZone() +" and Locale:" + formatter.getLocale() );
System.out.println("instant:" + instant );
System.out.println("output:" + output );
跑步時
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例外的"不支持的字段"指向例如年,是非常鈍的。也許應該檢測到這種情況,并且應該拋出直接指向Instant中缺少的區(qū)域id的異常!
更奇怪的是,如果你包括.withZone(例如.withZone(ZoneId.of("Z")))并格式化LocalDateTime,那么該區(qū)域就是IGNORED!因此,只要包含.withZone(),就可以對Instant和LocalDateTime使用相同的格式化程序,而不會影響后者顯示的時間。
為什么接受Instant有一個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()));
}
雖然此代碼可能會回答這個問題,但提供有關(guān)如何以及為何解決問題的其他背景將提高答案的長期價值。
雖然此代碼段可以解決問題,但包括解釋確實有助于提高帖子的質(zhì)量。請記住,您將來會回答讀者的問題,而這些人可能不知道您的代碼建議的原因。
Instant類不包含區(qū)域信息,它僅存儲UNIX紀元的時間戳(以毫秒為單位),即UTC的1月1日1070。
因此,格式化程序無法打印日期,因為日期始終打印為具體時區(qū)。
您應該將時區(qū)設(shè)置為格式化程序,一切都會好的,如下所示:
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。但是,其他一些語言的時間框架可能不支持毫秒,所以你應該這樣做
DateTimeFormatter.ISO_INSTANT.format(Instant.now().truncatedTo(ChronoUnit.SECONDS))
你是什??么意思"其他語言的時間框架"? ISO_INSTANT.format()會自動截斷為秒嗎?
有些框架只希望時間達到秒,因為它們不遵循完整的ISO約定,并且當輸入字符串的一部分有毫秒時就會中斷。
或者,如果您仍想使用從模式創(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);
我相信這可能會有所幫助,您可能需要使用某種localdate變體而不是即時變體
總結(jié)
以上是生活随笔為你收集整理的instant java,关于java:Format Instant to String的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: YOLOv5损失函数定义
- 下一篇: LNK2019 无法解析的外部符号 __