【问题标题】:How to custom format a FileTime如何自定义格式化 FileTime
【发布时间】:2016-01-30 08:32:30
【问题描述】:

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

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

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

抛出UnsupportedTemporalTypeException: Unsupported field: Year

【问题讨论】:

  • 你的意思是yyyy而不是uuuu
  • @Kal 我都试过了。 yyyy 只是抛出 Unsupported field: Year of Era 而不是 Unsupported field: Year
  • 你试过toMillis而不是toInstant吗?格式化 Instant 需要时区。
  • @Kal 我试过this。如何处理toMillis 的结果?
  • 啊是的。我认为您最好的选择是将时区附加到 DateTimeFormatter -- stackoverflow.com/questions/25229124/format-instant-to-string

标签: java date-formatting java-time


【解决方案1】:

您不能使用查询年份的DateTimeFormatter 实例来格式化 Instant。

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

您必须至少将Instance 转换为LocalDateTime

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

【讨论】:

    【解决方案2】:

    如果你的时间是这样的

    2015-01-01T10:10:09
    

    使用

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

    【讨论】:

    • 这不是我问的。
    【解决方案3】:

    格式化Instant 需要一个时区。这可以使用withZone(ZoneId)来实现:

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

    【讨论】:

      【解决方案4】:

      我个人认为错误消息“不支持的字段:年份”具有误导性。 真正的原因是缺少时区。需要此信息来帮助格式化程序在内部将给定的瞬间转换为人类时间表示。 解决方案:提供时区。然后支持格式化或解析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(...) 的调用,以防万一偏移量 - 你真的应该省略那个调用,因为否则格式化程序不会使用你输入的时区偏移量,而是使用提供的一个区域(我在自己的测试中观察到的一个陷阱)。

      【讨论】:

        【解决方案5】:

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

        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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-12-09
          • 2014-02-13
          • 2023-03-22
          • 2017-08-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-26
          相关资源
          最近更新 更多