java Joda 时间中的 LocalDate 间隔

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4387549/
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-10-30 06:08:42  来源:igfitidea点击:

LocalDate interval in Joda-time

javajodatime

提问by Wouter Coekaerts

Joda-time has an Intervalclass, which is a range between DateTimes. What can be used for a range of LocalDates?

Joda-time 有一个Interval类,它是DateTime之间的范围。什么可用于一系列LocalDate

I want an object that represents, for example "from 1/1/2011 to 10/1/2011", without the time (or timezones) ever coming into the picture.

我想要一个对象,例如“从 1/1/2011 到 10/1/2011”,没有时间(或时区)出现在图片中。

As far as I see, joda-time doesn't have anything built in for that. If there doesn't exist anything for it, and we'd create it, what should it look like? Most importantly, which interfaces from joda-time could it implement to best integrate into the other types, staying consistent with the joda-time design? Would it make sense for it to implement ReadableInterval, where getStartand getEndreturn DateMidnight?

据我所知,joda-time 没有为此内置任何东西。如果它不存在任何东西,而我们会创建它,它应该是什么样子?最重要的是,它可以实现哪些来自 joda-time 的接口以最好地集成到其他类型中,并与 joda-time 设计保持一致?它ReadableInterval在何处实施getStartgetEnd返回DateMidnight是否有意义?

采纳答案by olivr

I personnaly use the Rangeclass from Guava.

我个人使用Guava的Range类。

It supports open ended ranges. It is also possible to specify included or excluded bounds. Among other numerous possibilities, those allow to easily represent "before a date" or "after a date".

它支持开放式范围。也可以指定包含或排除的边界。在其他众多可能性中,那些允许轻松表示“日期之前”或“日期之后”。

Example for open-ended intervals.

开放式区间的示例。

Range<LocalDate> before2010 = Range.atMost(new LocalDate("2009-12-31"));
Range<LocalDate> alsoBefore2010 = Range.lessThan(new LocalDate("2010-01-01"));

It also offerts easy testing predicates, like contains and containsAll, and an intersection operation. All this tested and maintained.

它还提供了简单的测试谓词,如 contains 和 containsAll,以及一个交集操作。所有这些都经过测试和维护。

回答by Olivier Heidemann

The Intervalclass represents an interval of time from one millisecond instant to another instant (complete with timezone). DateMidnightis such an instant. So your proposed implementation of an interval for DateMidnights is allready there.

区间类表示的时间从一个毫秒时刻到另一时刻(完整的时区)的间隔。DateMidnight就是这样一个瞬间。因此,您提议的 DateMidnights 间隔实现已经准备就绪。

Interval i = new Interval(
    new DateMidnight(2010, 3, 2), new DateMidnight(2010, 3, 5));

i.contains(new DateTime(2010, 3, 1, 23, 59, 59, 999)); // == false
i.contains(new DateTime(2010, 3, 2, 0, 0, 0, 0)); // == true
i.contains(new DateTime(2010, 3, 4, 23, 59, 59, 999)); // == true
i.contains(new DateTime(2010, 3, 5, 0, 0, 0, 0)); // == false

However the concept of your searched interval for LocalDatesaka a Partial (without timezones) is not yet existent. If you're going to implement it for yourself, don't implement any interval interfaces at all. It would interfere with your wishes, because it is, as stated before, based on instants.

但是,您搜索的LocalDates也称为 Partial(无时区)的间隔的概念尚不存在。如果你打算自己实现它,根本不要实现任何间隔接口。它会干扰你的愿望,因为如前所述,它是基于瞬间的。

回答by Jon Skeet

EDIT: Oops, misread that before.

编辑:哎呀,之前误读了。

No, there's nothing equivalent in Joda Time that I'm aware of.

不,我所知道的 Joda Time 中没有任何等效的东西。

Personally I would just create my own class which included the start and end date: LocalDateIntervalor something like that. I wouldn'treuse Intervalfor this, as it would be confusing the concepts: a pair of LocalDatevalues doesn'trepresent two instants, as a local date isn't really an instant.

就我个人而言,我只会创建自己的类,其中包括开始和结束日期:LocalDateInterval或类似的东西。我不会Interval为此重用,因为它会混淆概念:一对LocalDate不代表两个瞬间,因为本地日期并不是真正的瞬间。

Personally I don't find Intervalparticularly useful in Joda Time... I'd been considering removing it from Noda Time. Now it sounds like I shouldn't ;)

就我个人而言,我觉得它Interval在 Joda Time 中不是特别有用……我一直在考虑将它从 Noda Time 中删除。现在听起来我不应该;)

回答by Michael Nascimento Santos

Note that using DateMidnightwill fail at the DST switch. It is better to convert using date.toDateTimeAtStartOfDay().

请注意,DateMidnight在 DST 开关处使用将失败。最好使用date.toDateTimeAtStartOfDay().

回答by Dmitry Serdiuk

I needed intervals of LocalDate objects too but it's not supported by JodaTime. So I had to create my own implementation. Later I've decided to polish it a bit and make open source.

我也需要 LocalDate 对象的间隔,但 JodaTime 不支持它。所以我必须创建自己的实现。后来我决定稍微打磨一下,开源。

You can check it out: https://github.com/serddmitry/joda-interval(available on Maven Central).

您可以查看:https: //github.com/serddmitry/joda-interval(可在 Maven Central 上获得)。

回答by Stimpson Cat

public class LocalDateInterval {

   private LocalDate begin;
   private LocalDate end;

   public LocalDateInterval(LocalDate begin, LocalDate end) {
     this.begin = begin;
     this.end = end;
   }



}

This works for me.

这对我有用。