java.time相当于Joda-Time`withTimeAtStartOfDay`? (获得当天的第一个时刻) [英] java.time equivalent of Joda-Time `withTimeAtStartOfDay`? (get first moment of the day)

查看:3553
本文介绍了java.time相当于Joda-Time`withTimeAtStartOfDay`? (获得当天的第一个时刻)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Joda-Time 库中, DateTime 类提供了一种方法 withTimeAtStartOfDay 获得第一时刻那天。你可能认为那个时刻是午夜。第一时间通常是 00:00:00.000 但不总是。

In the Joda-Time library, the DateTime class offers a method withTimeAtStartOfDay to get the first moment of the day. You might think of that moment as "midnight". That first moment is usually the time 00:00:00.000 but not always.

java.time包具有等效功能?

推荐答案

相当于使用一种特殊的方法, atStartOfDay ,在类 LocalDate

The equivalent is using a special method, atStartOfDay, in the class LocalDate:

ZoneId zoneId = ZoneId.of("America/New_York");
ZonedDateTime zdt = LocalDate.now(zoneId).atStartOfDay(zoneId);

另请注意,相当于 Joda-time DateTime LocalDateTime ,但 ZonedDateTime zoneId 参数在这里。有关迁移的具体示例 - 另请参见时区网站,了解有关<巴西的夏令时间(DST)转换:href =https://en.wikipedia.org/wiki/Daylight_saving_time =nofollow:

Please also note that the equivalent of Joda-Time DateTime is not LocalDateTime, but ZonedDateTime. The zoneId parameter matters here. Concrete example for migration - see also timezone website for details about Daylight Saving Time (DST) transition in Brazil:

Joda-Time(旧方式)

DateTime dt = 
  new DateTime(2015, 10, 18, 12, 0, DateTimeZone.forID("America/Sao_Paulo"));
dt = dt.withTimeAtStartOfDay();
System.out.println(dt); // 2015-10-18T01:00:00.000-02:00

请注意,这段代码甚至在第一行的午夜抛出异常,调用构造函数

Note that this code would even throw an exception for midnight in first line with call to constructor.

java.time(new way)

ZoneId zoneId = ZoneId.of("America/Sao_Paulo");
ZonedDateTime zdt =
  ZonedDateTime.of(2015, 10, 18, 12, 0, 0, 0, zoneId);
zdt = zdt.toLocalDate().atStartOfDay(zoneId);
System.out.println(zdt); // 2015-10-18T01:00-02:00[America/Sao_Paulo]

第二个程序声明的行为与Joda-Time的行为不同,因为它不会抛出异常,而是将当地时间按照所讨论的差距大小进行移动,这里有一个小时。这意味着,如果你选择午夜,结果将是一样的(即1:00)。如果你选择00:30,那么结果将是01:30。上面给出的示例选择中午作为输入。

The second program statement behaves differently than Joda-Time because it will not throw an exception but silently shifts the local time by the size of the gap in question, here one hour. This means, if you had chosen midnight, the result would be the same (namely at 1:00). If you had chosen 00:30, then the result would be 01:30. The example given above chooses noon as input.

引用 ZonedDateTime.of(...)


在大多数情况下,本地日期时间只有一个有效的偏移量。在重叠的情况下,当设置时钟时,有两个有效的偏移量。该方法使用通常对应于summer的较早的偏移量。

In most cases, there is only one valid offset for a local date-time. In the case of an overlap, when clocks are set back, there are two valid offsets. This method uses the earlier offset typically corresponding to "summer".

在间隙的情况下,当时钟向前跳转时,没有有效的偏移量。相反,本地日期时间被调整为稍后间隔的长度。对于典型的1小时夏令时更改,本地日期时间将在一小时后移动到通常对应于夏季的偏移量。

In the case of a gap, when clocks jump forward, there is no valid offset. Instead, the local date-time is adjusted to be later by the length of the gap. For a typical one hour daylight savings change, the local date-time will be moved one hour later into the offset typically corresponding to "summer".

由于这两个库太不相同,100%迁移所有细节,如异常行为和应用的DST转换策略是不可能的。但是这是您的指导:

A 100%-migration of all details like exception behaviour and applied DST transition strategies is not possible because both libraries are too different. But that is your guideline:


  • 替换 DateTime by ZonedDateTime

  • 考虑切换到 LocalDate 进行中间计算(见示例)

  • 使用对时区的明确引用,并将 DateTimeZone 替换为 ZoneId

  • replace DateTime by ZonedDateTime
  • consider switching to LocalDate for intermediate calculations (see example)
  • use explicit references to the timezone and replace DateTimeZone by ZoneId

这篇关于java.time相当于Joda-Time`withTimeAtStartOfDay`? (获得当天的第一个时刻)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆