java-在两个日期之间迭代并存储它们之间的所有10分钟

java-在两个日期之间迭代并存储它们之间的所有10分钟,java,date,Java,Date,我想在两个特定日期之间迭代,并将所有对应的10分钟值存储在ArrayList中。我遵循了线程中的答案和代码示例,但在根据需要定制代码时遇到了问题。到目前为止,我的代码如下: final DateFormat df = DateFormat.getDateTimeInstance(); final Calendar c = Calendar.getInstance(); c.clear(); for (c.set(StringDateUtils.getYear(earlie

我想在两个特定日期之间迭代,并将所有对应的10分钟值存储在ArrayList中。我遵循了线程中的答案和代码示例,但在根据需要定制代码时遇到了问题。到目前为止,我的代码如下:

final DateFormat df = DateFormat.getDateTimeInstance();
    final Calendar c = Calendar.getInstance();
    c.clear();
    for (c.set(StringDateUtils.getYear(earliestKey), (StringDateUtils.getMonth(earliestKey) - 1), 
            StringDateUtils.getDayOfMonth(earliestKey), StringDateUtils.getHourOfDay(earliestKey), 
            StringDateUtils.getMinuteOfHour(earliestKey));

            c.get(Calendar.YEAR) <= StringDateUtils.getYear(latestKey) && 
            c.get(Calendar.MONTH) <= (StringDateUtils.getMonth(latestKey) - 1) && 
            c.get(Calendar.DAY_OF_MONTH) <= StringDateUtils.getDayOfMonth(latestKey) && 
            c.get(Calendar.HOUR) <= StringDateUtils.getHourOfDay(latestKey) && 
            c.get(Calendar.MINUTE) <= StringDateUtils.getMinuteOfHour(latestKey);

            c.add(Calendar.MINUTE, 10)) {
        System.out.println(df.format(c.getTime()));
    }
final DateFormat df=DateFormat.getDateTimeInstance();
最终日历c=Calendar.getInstance();
c、 清除();
对于(c.set(StringDateUtils.getYear(earliestKey),(StringDateUtils.getMonth(earliestKey)-1),
StringDateUtils.getDayOfMonth(earliestKey),StringDateUtils.getHourOfDay(earliestKey),
getMinuteOfHour(earliestKey));

c、 get(Calendar.YEAR)循环条件无效。与复合值进行比较时,只需在前面的所有值相等时比较辅助值

例如,如果结束值为
endA
endB
endC
,则条件应为:

a < endA || (a == endA && (b < endB || (b == endB && c <= endC)))
如果使用Java 8+,则应使用新的
Java.time

final DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
ZonedDateTime endDateTime = StringDateUtils.getZonedDateMinute(latestKey);
ZonedDateTime dateTime = StringDateUtils.getZonedDateMinute(earliestKey);
for (; ! dateTime.isAfter(endDateTime); dateTime = dateTime.plusMinutes(10)) {
    System.out.println(dateTime.format(formatter));
}

您需要将
for
循环声明末尾的
替换为
{
你的方法就像一个符咒。投票关闭as无法复制/打字。@DasbLinkedLight不正确。如果开始是
2015-11-20 00:00
,结束是
2015-11-31 09:50
,它将在
2015-11-20 09:50
后停止,当小时条件第一次变为假。谢谢你的回答,我将更新StringDateUTIL根据您的主张进行分类。只是想澄清一下,问题是我在for循环中始终使用c作为开始和结束条件?@PiXel1225不,您的问题是您的循环条件是
a
final DateFormat df = DateFormat.getDateTimeInstance();
final Calendar c = Calendar.getInstance();
StringDateUtils.clearAndSetYearToMinute(c, latestKey);
long endMillis = c.getTimeInMillis();
StringDateUtils.clearAndSetYearToMinute(c, earliestKey);
for (; c.getTimeInMillis() <= endMillis; c.add(Calendar.MINUTE, 10))
    System.out.println(df.format(c.getTime()));
final DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
ZonedDateTime endDateTime = StringDateUtils.getZonedDateMinute(latestKey);
ZonedDateTime dateTime = StringDateUtils.getZonedDateMinute(earliestKey);
for (; ! dateTime.isAfter(endDateTime); dateTime = dateTime.plusMinutes(10)) {
    System.out.println(dateTime.format(formatter));
}