- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个格式化程序如下:
private static PeriodFormatter formatter = new PeriodFormatterBuilder()
.printZeroNever()
.appendYears().appendSuffix(" years ")
.appendMonths().appendSuffix(" months ")
.appendWeeks().appendSuffix(" weeks ")
.appendDays().appendSuffix(" days ")
.appendHours().appendSuffix(" hours ")
.appendMinutes().appendSuffix(" minutes ")
.appendSeconds().appendSuffix(" seconds")
.toFormatter();
并按如下方式使用它:
DateTime dt = DateTime.parse("2010-06-30T01:20");
Duration duration = new Duration(dt.toInstant().getMillis(), System.currentTimeMillis());
Period period = duration.toPeriod().normalizedStandard(PeriodType.yearMonthDayTime());
formatter.print(period);
输出是:
2274 days 13 hours 59 minutes 39 seconds
那么岁月在哪里呢?
最佳答案
这里的根本问题是您首先使用Duration
,IMO。 Duration
只是几毫秒...考虑其中的年数有点麻烦,因为一年要么是 365 天,要么是 366 天(甚至这取决于日历系统)。这就是为什么 toPeriod
method you're calling明确表示:
Only precise fields in the period type will be used. Thus, only the hour, minute, second and millisecond fields on the period will be used. The year, month, week and day fields will not be populated.
那么您调用normalizedStandard(PeriodType)
其中包括:
The days field and below will be normalized as necessary, however this will not overflow into the months field. Thus a period of 1 year 15 months will normalize to 2 years 3 months. But a period of 1 month 40 days will remain as 1 month 40 days.
不要从 Duration
创建周期,而是直接从 DateTime
和“now”创建,例如
DateTime dt = DateTime.parse("2010-06-30T01:20");
DateTime now = DateTime.now(); // Ideally use a clock abstraction for testability
Period period = new Period(dt, now, PeriodType.yearMonthDayTime());
关于java - joda time periodFormatter 不打印年份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39588030/
我是 grails 的新手。我正在使用 joda 插件来获取日期和时间。所以我在单独的列中显示日期和时间。现在我想使用条件搜索日期。 我在 gsp 页面中使用以下代码来搜索日期。
我试图在执行更多计算之前简单地将 TimeZone 信息添加回 LocalDate。 LocalDate 来自使用 ObjectLab LocalDateCalculator 将天数添加到现有 Dat
所以我在使用 JodaTime 年表 IslamicChronology 解析日期时遇到问题,因此编写了一个小示例来演示我的问题。 代码如下: import org.joda.time.Chronol
当我启动我的应用程序时,virgo 会抛出这样的异常: [2015-09-05 11:27:53.254] TCP Connection(2)-127.0.0.1 Installation of
我使用 Spring 从 Web API rest API 检索 JSON 数据,使用 Jackson Joda 模块反序列化 ISO 日期,并将其转换为 Joda DateTime。 Spring
在 Android 设备 (7.0) 上,出现此崩溃: Fatal Exception: org.joda.time.IllegalInstantException: Illegal instant
我需要将 GMT 转换为亚利桑那时间。亚利桑那州目前有 MST(夏令时关闭)。 但是在 Joda Time DateTimeZone 代码中,MST 已映射到 America/Denver: map.
大家早上好。 我想帮助您了解如何使用 1.2.1.1 版本的 Joda Time 完成 org.joda.time.DateTime 到 java.util.Date 的转换。 为什么选择 Joda
试试这个: @Type(type = "org.jadira.usertype.moneyandcurrency.joda.PersistentMoneyAmountAndCurrency") pri
尝试使用 Joda 将包含日期字符串的 JSON 字符串反序列化为 POJO 时遇到异常。 我将 Jackson2 与 Spring 和 Robospice 一起使用。 我遇到以下异常: Could
我有一个招摇的OpenAPI YAML文件,我想使用gradle-swagger-generator-plugin生成Java 这是我的config.json { "useRxJava2": tr
我想使用以下 MDX 语句: with member [x] as now()->plusMonths(1)->withDayOfMonth(1)->minusDay(1) select [x] on
System.out.println( PeriodFormat.getDefault().print(Period.hours(1).plusMinutes(30).plusSeconds(60))
我正在使用 Joda-Time Duration 来获取两个 DateTime 之间的持续时间: DateTime startTimeDate = new DateTime(startTimeDate
当我运行以下代码片段时: import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import java.sql.Times
我正在尝试在 Joda DateTime 中转换毫秒。米利斯是 1338501600000我使用了在线转换器和其他库,所有结果都是 1338501600000 millis 是 Fri Jun 01
我尝试使用以下代码,但收到错误格式无效:“12/11/2013”: String dFrom = ps.utils.gv(request, "dFrom"); String dTo = ps.ut
我正在尝试使用 Joda-Time 的持续时间类来计算两天之间的确切天数差异。这意味着我希望结果是 25.48 天,而不是仅仅 25 天。现在我有: private static final doub
有没有隐藏的方法来获得 Joda-Time以分钟为单位的时间段(或任何其他) 现在我做的是: (period.getHours()*60 + period.getMinutes() + roundTw
有谁知道如何解析日期,例如:Mon Aug 04 16:07:00 CEST 2014使用 Joda 的 DateTime 格式化程序转换为 dd/MM/YYYY HH:MM:SS。我已经尝试过了:
我是一名优秀的程序员,十分优秀!