如何自定义格式的FileTime

问题描述 投票:8回答:5

给出FileTime fileTime,如何以自定义方式将其格式化为字符串?

[String s = fileTime.toString()仅以ISO格式提供。

String s = DateTimeFormatter.ofPattern("uuuu-MMM-dd HH:mm:ss")
                              .format(fileTime.toInstant());

抛出UnsupportedTemporalTypeException: Unsupported field: Year

java date-formatting java-time
5个回答
4
投票

您无法使用查询年份的DateTimeFormatter实例格式化Instant。

Instant代表时间线上的单个点。这就是为什么不可能对“什么是年/日/时?”这个问题给出正确/唯一的答案。这取决于在世界上哪个地方问这个问题:在纽约,它不同于悉尼。但是您的DateTimeFormatter恰恰在问这个问题。这就是为什么您得到一个UnsupportedTemporalTypeException

您必须至少将Instance转换为LocalDateTime

System.out.println(timestampFormatter.format(
    LocalDateTime.ofInstant(fileTime.toInstant(), ZoneId.systemDefault()));

6
投票

我个人发现错误消息“不支持的字段:年份”具有误导性。 真正的原因是缺少时区。需要此信息来帮助格式化程序在内部将给定的时刻转换为人类时间表示形式。 解决方案:提供时区。然后支持格式化或解析Instant-与@flo的答案相反。

打印中:

String s = 
  DateTimeFormatter.ofPattern("uuuu-MMM-dd HH:mm:ss", Locale.ENGLISH)
    .withZone(ZoneId.systemDefault())
    .format(Instant.now());
System.out.println(s); // 2015-Oct-30 15:22:32

解析中:

不幸的是,反向过程-解析-不能以相同的直接方式工作,因为java.time的格式引擎设计为使得格式化程序仅返回原始的TemporalAccessor,需要将其转换为实际所需的类型。示例:

Instant instant =
  Instant.from(
    DateTimeFormatter.ofPattern("uuuu-MMM-dd HH:mm:ss", Locale.ENGLISH)
    .withZone(ZoneId.systemDefault())
    .parse("2015-Oct-30 15:22:32"));
System.out.println("=>" + instant); // 2015-10-30T14:22:32Z

如果要解析的输入包含时区偏移量或标识符,那么您可以修改模式(符号x,X,z,Z,VV等),并忽略对withZone(...)的调用,如果存在偏移量-您确实应该忽略该调用,因为否则格式化程序将不会使用输入的时区偏移,而是使用提供的一个区域(我在自己的测试中观察到的陷阱)。


2
投票

格式化Instant需要一个时区。可以使用withZone(ZoneId)

实现
String s = DateTimeFormatter.ofPattern("uuuu-MMM-dd HH:mm:ss")
                 .withZone(ZoneId.systemDefault())
                 .format(fileTime.toInstant());

1
投票

如果您的时间看起来像这样

2015-01-01T10:10:09

使用

yyyy-MM-dd'T'HH:mm:ss

1
投票

ZonedDateTime可以解析从FileTime.toString()获得的默认字符串:(在下面的代码段中提供您自己的“路径”)

FileTime fileTime = Files.getLastModifiedTime(path);
ZonedDateTime zonedDateTime = ZonedDateTime.parse(fileTime.toString());
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEEE, MMMM d, yyyy   HH:mm:ss");   
System.out.println(dtf.format(zonedDateTime));

结果:2020年4月18日,星期六13:43:29

© www.soinside.com 2019 - 2024. All rights reserved.