Java Joda - 如何在不忽略 TimeZones 的情况下比较两个 DateTime 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18304105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:18:33  来源:igfitidea点击:

Joda - How to compare two DateTime objects without ignoring TimeZones

javadatetimetimezonejodatimeepoch

提问by Tiago

I am having a hard time comparing two DateTime objects that are in different TimeZones.

我很难比较位于不同 TimeZones 中的两个 DateTime 对象。

What I already know:

我已经知道的:

1) I know that the "isBefore()"method does nottake TimeZones into account. So, the "if" condition bellow is not true (even though I would like it to be true):

1)我知道,“isBefore()”方法并没有采取时区考虑在内。所以,下面的“if”条件不成立(即使我希望它是真的):

long aRandomTimeSinceTheEpoch = 1234567789L;

String TIMEZONE_SYDNEY_AUSTRALIA = "Australia/Sydney";
DateTimeZone sydneyTimeZone = DateTimeZone.forID(TIMEZONE_SYDNEY_AUSTRALIA);
Chronology chronologySydney = GJChronology.getInstance(sydneyTimeZone);

String TIMEZONE_NEWYORK = "America/New_York";
DateTimeZone newYorkTimeZone = DateTimeZone.forID(TIMEZONE_NEWYORK);
Chronology chronologyNewYork = GJChronology.getInstance(newYorkTimeZone);

DateTime sydneyDateTime = new DateTime(aRandomTimeSinceTheEpoch, chronologySydney);
DateTime newYorkDateTime = new DateTime(aRandomTimeSinceTheEpoch, chronologyNewYork);

if( newYorkDateTime.isBefore(sydneyDateTime) ){
    System.out.println("true");
}

2) Based on this answer (https://stackoverflow.com/a/8793980) it seems the correct way to do it is by Period instead, as Period is the right concept for what I am trying to do. However, that code throws an "UnsupportedOperationException - if the period contains years or months"exception sometimes (because I am dealing with dates that can be up to 2 years away from each other).

2)根据这个答案(https://stackoverflow.com/a/8793980),正确的做法似乎是通过 Period 来代替,因为 Period 是我想要做的事情的正确概念。但是,该代码有时会抛出“UnsupportedOperationException - 如果该期间包含年或月”异常(因为我正在处理彼此相距最多 2 年的日期)。

In short, all I want is an "isBefore()"method that takes TimeZones into account.(and that does not throw exceptions like the one above). How can I achieve this in Joda?

简而言之,我想要的只是一个将 TimeZones 考虑在内的“isBefore()”方法。(并且不会像上面那样抛出异常)。我怎样才能在 Joda 中实现这一目标?

采纳答案by Matt Johnson-Pint

What you're missing is that anytime you are measuring in seconds or milliseconds from the epoch - that is always in UTC.

您缺少的是,无论何时您都以秒或毫秒为单位进行测量 - 这始终是 UTC。

So your sydneyDateTimeand newYorkDateTimemay have different zones, but since they are both originating from the same aRandomTimeSinceTheEpochvalue, then they both occur at the same exact moment. Thus neither is beforethe other.

因此,您的sydneyDateTimenewYorkDateTime可能有不同的区域,但由于它们都源自相同的aRandomTimeSinceTheEpoch值,因此它们都发生在同一时刻。因此,两者都不另一个之前

By analogy, it's like asking which is greater, 1 inch or 2.54 centimeters?

以此类推,这就像问哪个更大,1 英寸还是 2.54 厘米?

From your comments, it looks like you would like to compare the localtimes in each time zone, which you can do like this:

从您的评论来看,您似乎想比较每个时区的当地时间,您可以这样做:

if( newYorkDateTime.toLocalDateTime().isBefore(sydneyDateTime.toLocalDateTime()) )

Note that if you are always starting from the same source instant, then this value will always be true. Just like 2.54 is always greater than 1.

请注意,如果您始终从同一个源时刻开始,则此值将始终为真。就像 2.54 总是大于 1 一样。

回答by jgm

use getMillis()on each date to obtain the milliseconds and compare those, that will give you an absolute pair of numbers against which to compare.

getMillis()在每个日期使用以获取毫秒数并进行比较,这将为您提供一对绝对的数字来进行比较。