相关疑难解决方法(0)

格式化即时字符串

我正在尝试使用新的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)

java datetime datetime-format java-8 java-time

192
推荐指数
5
解决办法
14万
查看次数

获取文件的最后修改日期/时间作为本地日期/时间字符串

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 是如何工作的。

java file-attributes java-8

6
推荐指数
1
解决办法
9879
查看次数