推荐用于Joda-Time的DateMidnight [英] Recommended use for Joda-Time's DateMidnight

查看:350
本文介绍了推荐用于Joda-Time的DateMidnight的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

LocalDate#toDateMidnight 的javdoc如下所示:


从v1.5,建议避免使用DateMidnight,并使用
toDateTimeAtStartOfDay(),因为这个异常详细描述了



此方法将抛出如果默认时区在午夜将
切换到夏令时,则此例外,此LocalDate表示切换日期的
。问题是在所需的日期没有这样的时间
午夜,因此抛出异常。


午夜在某些时区不存在的事实似乎足以避免完全使用 DateMidnight (假设你的代码没有使用已知不是固定的时区,有这个DST情况,将来永远不需要使用不同的时区)。



但是, DateMidnight 是不被弃用,并且在javadoc中没有类似的推荐或警告,对于 DateMidnight 类本身。此外, DateMidnight 构造函数乐意接受即时和时区,使得午夜在给定的日期不存在,而不是抛出 IllegalArgumentException like LocalDate#toDateMidnight
$ b在$ {code> DateMidnight 的行为就像一个 DateTime $ b

当某一天不存在午夜时,为什么 LocalDate#toDateMidnight 抛出异常,而 DateMidnight 构造函数不?如果有的话, DateMidnight 的推荐用例是什么?

解决方案

使用 DateMidnight 不是很好的理由。 LocalDate 是更好的选择。这是因为午夜在某些时区每年不会发生一次,完全弄乱了课堂的可用性,并在应用程序中创建了错误。



构造函数已修复为避免最坏的问题,但是看到内部毫秒值指向01:00的 DateMidnight 对象并不是很好。


The javdoc for LocalDate#toDateMidnight reads as follows:

As from v1.5, you are recommended to avoid DateMidnight and use toDateTimeAtStartOfDay() instead because of the exception detailed below.

This method will throw an exception if the default time zone switches to Daylight Savings Time at midnight and this LocalDate represents that switchover date. The problem is that there is no such time as midnight on the required date, and as such an exception is thrown.

The fact that midnight does not exist in certain time zones seems like reason enough to avoid using DateMidnight entirely (assuming your code is not using a fixed time zone that is known not to have this DST situation and will never need to use different time zones in the future).

However, DateMidnight is not deprecated and there is no similar recommendation or warning in the javadoc for the DateMidnight class itself. Furthermore, the DateMidnight constructor happily accepts an instant and time zone such that midnight does not exist on the given day, rather than throwing an IllegalArgumentException like LocalDate#toDateMidnight. The resulting DateMidnight behaves like a DateTime with time at start of day.

When midnight does not exist on a given day, why does LocalDate#toDateMidnight throw an exception while the DateMidnight constructor does not? What is the recommended use case for DateMidnight if any?

解决方案

There is no good reason to use DateMidnight. LocalDate is the better option. Thats because midnight does not occur once a year in certain time-zones, completely messing up the usability of the class, and creating bugs in applications.

The constructor was fixed to avoid the worst problem, however seeing a DateMidnight object with the internal millisecond value pointing at 01:00 isn't exactly great.

这篇关于推荐用于Joda-Time的DateMidnight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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