【问题标题】:Convert threeten LocalDate to YearMonth将三个 LocalDate 转换为 YearMonth
【发布时间】:2014-03-18 10:05:24
【问题描述】:

我有一个LocalDate,我需要将其转换为YearMonth。 我正在使用 ThreeTen API(JSR-310 到 Java SE 7 的反向移植)。 无论如何我可以做到这一点吗?

我在 Joda-Time 中尝试过,但在 ThreeTen 中没有提供。

LocalDate date;
new YearMonth(date.getYear(), date.getMonthOfYear());

【问题讨论】:

    标签: java date java-time


    【解决方案1】:

    查看文档YearMonth api,尝试:

    YearMonth myYearMonth = YearMonth.from(localDate);
    

    【讨论】:

    • ThreeTen 文档中指向 YearMonth 的直接 URL。
    • @Squizer,您的网址已损坏,我已对其进行了编辑。如果不是您想要的,请再次更新。
    • @YuchenZhong 谢谢
    【解决方案2】:

    这些命令都会产生相等的YearMonth 值:

    YearMonth.from(localDate)
    
    localDate.query(YearMonth.FROM) // ThreeTen
    localDate.query(YearMonth::from) // JDK 8
    
    YearMonth.of(localDate.getYear(), localDate.getMonth())
    
    YearMonth.of(localDate.getYear(), localDate.getMonthValue())
    

    【讨论】:

      猜你喜欢
      • 2021-10-24
      • 2019-07-22
      • 2019-05-23
      • 1970-01-01
      • 2012-02-18
      • 2018-07-15
      • 2020-04-10
      • 2015-11-07
      • 1970-01-01
      相关资源
      最近更新 更多