java 我需要一个循环遍历日期间隔

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

I need a cycle which iterates through dates interval

javaalgorithmdate

提问by Roman

I have the start date and the end date. I need to iterate through every day between these 2 dates.

我有开始日期和结束日期。我需要在这两个日期之间每天迭代。

What's the best way to do this?

做到这一点的最佳方法是什么?

I can suggest only something like:

我只能建议如下:

Date currentDate = new Date (startDate.getTime ());
while (true) {
   if (currentDate.getTime () >= endDate.getTime ())
      break;
   doSmth ();
   currentDate = new Date (currentDate.getTime () + MILLIS_PER_DAY);
}

回答by stacker

ready to run ;-)

准备运行;-)

public static void main(String[] args) throws ParseException {
    GregorianCalendar gcal = new GregorianCalendar();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
    Date start = sdf.parse("2010.01.01");
    Date end = sdf.parse("2010.01.14");
    gcal.setTime(start);
    while (gcal.getTime().before(end)) {
        gcal.add(Calendar.DAY_OF_YEAR, 1);
        System.out.println( gcal.getTime().toString());
    }
}

回答by Jay

Ditto on those saying to use a Calendar object.

同上那些说使用 Calendar 对象的人。

You can get into surprising trouble if you try to use a Date object and add 24 hours to it.

如果您尝试使用 Date 对象并向其添加 24 小时,您可能会遇到令人惊讶的麻烦。

Here's a riddle for you: What is the longest month of the year? You might think that there is no answer to that question. Seven months have 31 days each, so they are all the same length, right? Well, in the United States that would be almost right, but in Europe it would be wrong! In Europe, October is the longest month. It has 31 days and 1 hour, because Europeans set their clocks back 1 hour for Daylight Saving Time in October, making one day in October last 25 hours. (Americans now begin DST in November, which has 30 days, so November is still shorter than October or December. Thus making that riddle not as amusing for Americans.)

给你一个谜语:一年中最长的月份是哪一个月?你可能认为这个问题没有答案。七个月各有 31 天,所以它们的长度都是一样的,对吗?嗯,在美国这几乎是对的,但在欧洲就错了!在欧洲,十月是最长的月份。它有 31 天 1 小时,因为欧洲人在 10 月的夏令时将时钟调回 1 小时,使 10 月的一天持续 25 小时。(美国人现在在 11 月开始夏令时,有 30 天,所以 11 月仍然比 10 月或 12 月短。因此让这个谜语对美国人来说不那么有趣。)

I once ran into trouble by doing exactly what you're trying to do: I used a Date object and added 24 hours to it in a loop. It worked as long as I didn't cross Daylight Saving Time boundaries. But when I did, suddenly I skipped a day or hit the same day twice, because Midnight March 8, 2009 + 24 hours = 1:00 AM March 10. Drop off the time, as I was doing, and March 9 was mysteriously skipped. Likewise midnight Nov 1, 2009 + 24 hours = 11:00 PM Nov 1, and we hit Nov 1 twice.

我曾经因为完全按照您的要求执行操作而遇到了麻烦:我使用了一个 Date 对象并在循环中为其添加了 24 小时。只要我不跨越夏令时界限,它就可以工作。但是当我这样做时,突然间我跳过了一天或两次命中同一天,因为 2009 年 3 月 8 日午夜 + 24 小时 = 3 月 10 日凌晨 1:00。放下时间,正如我所做的那样,3 月 9 日被神秘地跳过了. 同样,2009 年 11 月 1 日午夜 + 24 小时 = 11 月 1 日晚上 11:00,我们两次击中了 11 月 1 日。

回答by Jherico

Use a Calendar object if you want to manipulate dates.

如果要操作日期,请使用 Calendar 对象。

    Calendar c = Calendar.getInstance();
    // ... set the calendar time ...
    Date endDate = new Date();
    // ... set the endDate value ...

    while (c.getTime().before(endDate) {
       // do something
       c.add(Calendar.DAY_OF_WEEK, 1);
    }

Or use Joda Time

或者使用Joda 时间