【发布时间】:2019-11-06 15:17:23
【问题描述】:
我目前正在将 Joda-Time 的一些代码更改为使用 Three-Ten Android Backport。
以下所有方法都将long 作为参数并返回long
使用 JodaTime 获得一周的结束和开始对于 Joda-Time 来说是直截了当的:
LocalDate(long time).dayOfWeek().withMaximumValue()
LocalDate(long time).dayOfWeek().withMinimumValue()
一天的结束和开始也存在同样的问题,Joda-Time:
DateTime(long time).withTimeAtStartOfDay().getMillis() + DateUtils.DAY_IN_MILLIS - 1
DateTime(long time).withTimeAtStartOfDay().getMillis()
但是我不明白如何以这种方式使用threeTenAbp。
一天的结束和开始的想法:
一天结束:
LocalDateTime dt = DateTimeUtils.toLocalDateTime(new Timestamp(time));
ZonedDateTime zdt = ZonedDateTime.of(dt, ZoneId.systemDefault());
return zdt.with(LocalTime.MAX).toEpochSecond();
一天的开始:
LocalDateTime dt = DateTimeUtils.toLocalDateTime(new Timestamp(long time));
ZonedDateTime zdt = ZonedDateTime.of(dt, ZoneId.systemDefault());
return zdt.toLocalDate().atStartOfDay(ZoneId.systemDefault()).toEpochSecond();
这似乎很复杂,并没有真正为我提供任何线索,告诉我如何获取与传递给函数的long time 相对应的一周开始和结束的时间。
【问题讨论】:
-
既然您正在重写,我建议:(1) 停止使用
long来表示日期和时间。使用适当的日期时间对象。 (2)不要得到一天的结束(那一刻无论如何都不存在)。取而代之的是第二天的开始。了解半开区间。 -
顺便说一句,您不想将 Joda-Time 的
DateTime替换为LocalDateTime。考虑ZonedDateTime。或OffsetDateTime。而且你不想引入过时的Timestamp类。
标签: android datetime jodatime threetenbp