我正在尝试使用新的java 8 time-api和模式将Instant格式化为String:
Instant instant = ...;
String out = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(instant);
Run Code Online (Sandbox Code Playgroud)
使用上面的代码我得到一个异常,它抱怨一个不受支持的字段:
java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: YearOfEra
at java.time.Instant.getLong(Instant.java:608)
at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298)
...
Run Code Online (Sandbox Code Playgroud) new File(url).lastModified()返回long等于自纪元以来的毫秒数(基于 GMT)。
将其转换为String代表系统本地日期/时间的简单方法是什么?
如果你真的需要看到我在这里的尝试,那就是,但这是一团糟,而且无论如何都是错误的:
LocalDateTime.ofEpochSecond(new File(url).lastModified()/1000,0,ZoneOffset.UTC).atZone(ZoneId.of("UTC")).format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG))
Run Code Online (Sandbox Code Playgroud)
除此之外,LocalDateTime我只是不知道时间 API 是如何工作的。