gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:06:10 24 4
gpt4 key购买 nike

我想在两个特定日期之间进行迭代,并将所有相应的 10 分钟值存储在 ArrayList 中。我遵循 Get all full hours of every day of a year 中的答案和代码示例线程,但我在根据我的需要自定义代码时遇到问题。到目前为止我的代码如下:

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()));
}

earliestKey 包含具有最早日期的字符串,latestKey 包含具有最新日期的字符串。 StringDateUtils 是一个自定义编写的类,其中包含从以 YYYYMMDD_HHMM 方式组装的字符串中获取年、月等的所有辅助方法。我还检查了 earliestKeylatestKey 的值在上述表单中是否有效,一切似乎都正常。据我所知,问题出在循环条件的第二部分,即经典循环迭代中的中断条件。我尝试过用 '!=' 代替不等式,或用 OR 代替 AND 运算符,但我无法让它工作。下面的代码被剪断,甚至连第一次迭代都不会进入。

最佳答案

循环条件无效。与复合值进行比较时,仅当所有前面的值相等时才需要比较次要值。

例如如果结束值为 endAendBendC,则条件应为:

a < endA || (a == endA && (b < endB || (b == endB && c <= endC)))

但是,为了获得更好的性能,您应该将结束条件解析为易于测试的单个值:

final DateFormat df = DateFormat.getDateTimeInstance();
final Calendar c = Calendar.getInstance();
c.clear();
c.set(StringDateUtils.getYear(latestKey),
StringDateUtils.getMonth(latestKey) - 1,
StringDateUtils.getDayOfMonth(latestKey),
StringDateUtils.getHourOfDay(latestKey),
StringDateUtils.getMinuteOfHour(latestKey));
long endMillis = c.getTimeInMillis();
c.clear();
c.set(StringDateUtils.getYear(earliestKey),
StringDateUtils.getMonth(earliestKey) - 1,
StringDateUtils.getDayOfMonth(earliestKey),
StringDateUtils.getHourOfDay(earliestKey),
StringDateUtils.getMinuteOfHour(earliestKey));
for (; c.getTimeInMillis() <= endMillis; c.add(Calendar.MINUTE, 10))
System.out.println(df.format(c.getTime()));

现在,由于 StringDateUtils 是一个自行开发的帮助程序类,因此您应该添加一个帮助程序来设置 Calendar,在这种情况下,您最终会得到:

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()));

如果使用 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));
}

关于java - 在两个日期之间迭代并存储它们之间的所有 10 分钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34727962/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com