java - 将 org.joda.time 转换为 java.time

标签 java java-time

我正在尝试在我们的应用程序中用 java.time 替换 org.joda.time 。 因为我们使用的是 org.joda.time 包,所以我们有 ISO8601 格式的周期和 org.joda.time.Period。

Period monthly = ISOPeriodFormat.standard().parsePeriod("P1M");
        Period yearly = ISOPeriodFormat.standard().parsePeriod("P1Y");
        Period weekly = ISOPeriodFormat.standard().parsePeriod("P1W");
        Period daily = ISOPeriodFormat.standard().parsePeriod("P1D");
        Period hour = ISOPeriodFormat.standard().parsePeriod("PT1H");
        Period minute = ISOPeriodFormat.standard().parsePeriod("PT1M");

由于我们的系统中存储了 P1Y,P1M,P1W,P1D 等格式,因此在 java.time 中寻找相同内容的格式化程序。

java.time.Duration 支持小时、分钟和天。

Duration dailyD = Duration.parse("P1D");
    Duration hourD = Duration.parse("PT1H");
    Duration minuteD = Duration.parse("PT5M");

但是,在 java.time 中看不到对周(P1W)、月(P1M)、年(P1Y)的支持。有没有其他方法可以将其转换为 java.time.Period 或 java.time.Duration?

最佳答案

持续时间是基于时间的时间量。

期间是基于日期的时间量。

对于持续时间,您尝试的正确性。请检查以下示例是否适合您的情况

周期 per1 = period.ofDays(-4);//P-4D

周期 per1a = period.ofDays(7);//P7D

周期 per2 = period.ofWeeks(5);//P5W

周期 per3 = period.ofMonths(-3);//P-3M

周期 per4 = period.ofYears(2);//P2Y

关于java - 将 org.joda.time 转换为 java.time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47215336/

相关文章:

java - Spring 3 为整个应用程序注册一次属性编辑器

java - Apache 通用 CSVParser/CSVRecord 为空字段返回 null

java - 使用 java.time/Java-8 根据语言环境从一周的第一天开始打印日历

java - 使用 DateTimeFormatter 为 "early"日期格式化时间戳

java - XSLT 扩展 : I am trying to use a custom java function in XSLT but am not able to pin point the mistake

java - 字段子类访问 - 最好的方法

java - 数据库表被锁定(Java、SQLite)

Java 1.8 时间功能

java-8 - 年和年代有什么区别?

jsp - JSTL LocalDateTime 格式