Java Joda Time - 实现日期范围迭代器

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

Java Joda Time - Implement a Date range iterator

javadateiteratorjodatime

提问by mickthompson

I'm trying to implement without success a Date iterator with Joda time.
I need something that allows me to iterate all the days form startDate to endDate
Do you have any idea on how to do that?

我正在尝试使用 Joda 时间实现 Date 迭代器但没有成功。
我需要一些可以让我从 startDate 到 endDate 迭代所有日子的东西
你知道怎么做吗?

回答by Jon Skeet

Here's something to get you started. You may want to think about whether you want it to be inclusive or exclusive at the end, etc.

这里有一些东西可以让你开始。您可能要考虑最后是希望它是包容性的还是排他性的,等等。

import org.joda.time.*;
import java.util.*;

class LocalDateRange implements Iterable<LocalDate>
{
    private final LocalDate start;
    private final LocalDate end;

    public LocalDateRange(LocalDate start,
                          LocalDate end)
    {
        this.start = start;
        this.end = end;
    }

    public Iterator<LocalDate> iterator()
    {
        return new LocalDateRangeIterator(start, end);
    }

    private static class LocalDateRangeIterator implements Iterator<LocalDate>
    {
        private LocalDate current;
        private final LocalDate end;

        private LocalDateRangeIterator(LocalDate start,
                                       LocalDate end)
        {
            this.current = start;
            this.end = end;
        }

        public boolean hasNext()
        {
            return current != null;
        }

        public LocalDate next()
        {
            if (current == null)
            {
                throw new NoSuchElementException();
            }
            LocalDate ret = current;
            current = current.plusDays(1);
            if (current.compareTo(end) > 0)
            {
                current = null;
            }
            return ret;
        }

        public void remove()
        {
            throw new UnsupportedOperationException();
        }
    }
}

class Test
{
    public static void main(String args[])
    {
        LocalDate start = new LocalDate(2009, 7, 20);
        LocalDate end = new LocalDate(2009, 8, 3);
        for (LocalDate date : new LocalDateRange(start, end))
        {
            System.out.println(date);
        }
    }
}

It's a while since I've written an iterator in Java, so I hopeit's right. I think it's pretty much okay...

我已经有一段时间没有用 Java 编写迭代器了,所以我希望它是对的。我觉得基本没问题...

Oh for C# iterator blocks, that's all I can say...

哦,对于 C# 迭代器块,我只能说......

回答by Ole V.V.

I know you asked about Joda-Time. Today we should prefer to use java.time, the modern Java date and time API that is basically a further development of Joda-Time. Since Java 9 the iteration of a date range has been built in through a Stream:

我知道你问过 Joda-Time。今天我们应该更喜欢使用 java.time,现代 Java 日期和时间 API,基本上是 Joda-Time 的进一步发展。自 Java 9 以来,日期范围的迭代已通过以下方式构建Stream

    LocalDate startDate = LocalDate.of(2019, Month.AUGUST, 28);
    LocalDate endate = LocalDate.of(2019, Month.SEPTEMBER, 3);
    startDate.datesUntil(endate).forEach(System.out::println);

Output:

输出:

2019-08-28
2019-08-29
2019-08-30
2019-08-31
2019-09-01
2019-09-02
2019-08-28
2019-08-29
2019-08-30
2019-08-31
2019-09-01
2019-09-02

If you wanted the end date to be inclusive, use datesUntil(endate.plusDays(1)).

如果您希望包含结束日期,请使用datesUntil(endate.plusDays(1)).

And if you literally wanted an Iterator:

如果你真的想要一个Iterator

    Iterator<LocalDate> ldi = startDate.datesUntil(endate).iterator();

The Joda-Time home page says:

Joda-Time 主页说:

Note that Joda-Time is considered to be a largely “finished” project. No major enhancements are planned. If using Java SE 8, please migrate to java.time(JSR-310).

请注意,Joda-Time 被认为是一个很大程度上“已完成”的项目。没有计划进行重大改进。如果使用 Java SE 8,请迁移到java.time(JSR-310)。

(Joda-Time - Home)

( Joda-Time - 主页)