jodatime:打印周期为 Days:Hours:Minutes:Seconds
JodaTime: Print Period as Days:Hours:Minutes:Seconds
奇怪的是,我遇到的问题与许多类似的帖子相反;我正在尝试使用 JodaTime(在 Android 上)给我一个 dd:hh:mm:ss 类型的输出并且失败得很惨。代码是;
public String GetEventTimeString(boolean withMS, int eventTime)
{
Period p = new Period(eventTime, PeriodType.yearMonthDayTime());
if (withMS)
return _formatter_withms.print(p);
else return _formatter_withoutms.print(p);
}
当我在设置 p
后使用调试器停止时,我看到 p 本身的小时值设置为 568,天数值为零,所以我没有发布创建的代码格式化程序。 eventTime 是一个 int,表示经过的时间(以毫秒为单位),在本例中的值为 2045489151。
如果数字的大小有些奇怪,而且它是一个整数而不是一个长整数,我尝试制作一个 new Period(1500000L, PeriodType.yearMonthDayTime())
并得到 0 天 25 小时 0 分钟和秒。
探索期间的 iType 成员的结构,我看到一个包含元素 "years"、"months" 等的数组 iTypes。我还看到一个数组 iIndices,它似乎将类型映射到元素其他一些数组。大多数索引都有意义,但 "Days" 对应的索引是 -1。所以似乎 PeriodType.yearMonthDayTime()
没有按照我认为文档所说的去做。
如果我尝试 PeriodType.yearWeekDayTime
,我会得到相同的结果。如果我尝试 PeriodType.yearWeekDay
那么这三个字段在我的 25 小时测试值中为零。因此,行为不端的不仅仅是一个 PeriodType。
想知道从我相当任意(选择一个数字并不断添加零)1500000ms 得到恰好 25 小时的巧合,我尝试了 1510000 并得到 25h 10m 0s。但是,当您手动进行转换时,这些实际上都没有任何意义!
这是在 Android 上,最新的 Android Studio 1.3.2,不确定如何判断我使用的是哪个版本的 JodaTime,但它是在过去几个月内,这似乎是有点太明显了,不像是一个晦涩的错误 - 我认为更多的是误解和我挖掘得太深的结合。
有没有人对发生的事情有任何想法?
嗯,Joda-Time 说 normalization of a millisecond-based period:
Creates a period from the given millisecond duration. Only precise
fields in the period type will be used. Imprecise fields will not be
populated.
问题来了:天单位是Joda-Time上下文中的精确字段吗?不,因为在许多国家/地区切换夏令时 on/off 一天可能有 23 或 25 小时(实际上还有更多的可能性,例如 23.5 小时等)。所以你调用的构造函数没有填充day-unit-field.
如何才能得到想要的归一化效果?只做这个推荐的方法(在构建周期时没有规范化):
long eventTime = 2045489151;
Period p = new Period(eventTime, PeriodType.millis());
System.out.println(p); // PT2045489.151S (no normalization)
System.out.println(p.normalizedStandard(PeriodType.dayTime()));
// P23DT16H11M29.151S
System.out.println(p.normalizedStandard(PeriodType.yearMonthDayTime()));
// P23DT16H11M29.151S ( the same result as one line before, not the best period type)
恕我直言,您使用的 Period
-构造函数进行了部分规范化,这是为了辩论。在我看来,要么使用给定的周期类型进行完全规范化,要么根本不进行规范化(我更喜欢 - 只是将毫秒保留为毫秒,但第二个构造函数参数简直是愚蠢的)将是一个更明智的设计决定。有趣的是(也令人困惑),没有显式句点类型参数的构造函数也进行了部分规范化。为避免构造函数中过早的不完全规范化,您必须将句点类型明确指定为 PeriodType.millis()
。很抱歉让您感到困惑。
奇怪的是,我遇到的问题与许多类似的帖子相反;我正在尝试使用 JodaTime(在 Android 上)给我一个 dd:hh:mm:ss 类型的输出并且失败得很惨。代码是;
public String GetEventTimeString(boolean withMS, int eventTime)
{
Period p = new Period(eventTime, PeriodType.yearMonthDayTime());
if (withMS)
return _formatter_withms.print(p);
else return _formatter_withoutms.print(p);
}
当我在设置 p
后使用调试器停止时,我看到 p 本身的小时值设置为 568,天数值为零,所以我没有发布创建的代码格式化程序。 eventTime 是一个 int,表示经过的时间(以毫秒为单位),在本例中的值为 2045489151。
如果数字的大小有些奇怪,而且它是一个整数而不是一个长整数,我尝试制作一个 new Period(1500000L, PeriodType.yearMonthDayTime())
并得到 0 天 25 小时 0 分钟和秒。
探索期间的 iType 成员的结构,我看到一个包含元素 "years"、"months" 等的数组 iTypes。我还看到一个数组 iIndices,它似乎将类型映射到元素其他一些数组。大多数索引都有意义,但 "Days" 对应的索引是 -1。所以似乎 PeriodType.yearMonthDayTime()
没有按照我认为文档所说的去做。
如果我尝试 PeriodType.yearWeekDayTime
,我会得到相同的结果。如果我尝试 PeriodType.yearWeekDay
那么这三个字段在我的 25 小时测试值中为零。因此,行为不端的不仅仅是一个 PeriodType。
想知道从我相当任意(选择一个数字并不断添加零)1500000ms 得到恰好 25 小时的巧合,我尝试了 1510000 并得到 25h 10m 0s。但是,当您手动进行转换时,这些实际上都没有任何意义!
这是在 Android 上,最新的 Android Studio 1.3.2,不确定如何判断我使用的是哪个版本的 JodaTime,但它是在过去几个月内,这似乎是有点太明显了,不像是一个晦涩的错误 - 我认为更多的是误解和我挖掘得太深的结合。
有没有人对发生的事情有任何想法?
嗯,Joda-Time 说 normalization of a millisecond-based period:
Creates a period from the given millisecond duration. Only precise fields in the period type will be used. Imprecise fields will not be populated.
问题来了:天单位是Joda-Time上下文中的精确字段吗?不,因为在许多国家/地区切换夏令时 on/off 一天可能有 23 或 25 小时(实际上还有更多的可能性,例如 23.5 小时等)。所以你调用的构造函数没有填充day-unit-field.
如何才能得到想要的归一化效果?只做这个推荐的方法(在构建周期时没有规范化):
long eventTime = 2045489151;
Period p = new Period(eventTime, PeriodType.millis());
System.out.println(p); // PT2045489.151S (no normalization)
System.out.println(p.normalizedStandard(PeriodType.dayTime()));
// P23DT16H11M29.151S
System.out.println(p.normalizedStandard(PeriodType.yearMonthDayTime()));
// P23DT16H11M29.151S ( the same result as one line before, not the best period type)
恕我直言,您使用的 Period
-构造函数进行了部分规范化,这是为了辩论。在我看来,要么使用给定的周期类型进行完全规范化,要么根本不进行规范化(我更喜欢 - 只是将毫秒保留为毫秒,但第二个构造函数参数简直是愚蠢的)将是一个更明智的设计决定。有趣的是(也令人困惑),没有显式句点类型参数的构造函数也进行了部分规范化。为避免构造函数中过早的不完全规范化,您必须将句点类型明确指定为 PeriodType.millis()
。很抱歉让您感到困惑。