简体   繁体   English

如何从一年中的日期向下迭代到上一年的日期?

[英]How to iterate downward from a Date in one year to a Date in a previous year?

I have a beginning date, let us say it is 2020-12-30 08:00:00, and I have an end date, let us say it is 2021-02-11 16:00:00.我有一个开始日期,假设是 2020-12-30 08:00:00,我有一个结束日期,假设是 2021-02-11 16:00:00。

I need to get the hours between these days which I do by using:我需要通过以下方式获取这些天之间的时间:

long diffInHours = TimeUnit.HOURS.convert(Math.abs(closeStoreDateTime.getTime() - openStoreDateTime.getTime()), TimeUnit.MILLISECONDS);

My issue is that as I iterate downward from the end date to the beginning date, I am converting the hour difference between the current hour and the beginning hour into an accurate time to store in a map later on.我的问题是,当我从结束日期向下迭代到开始日期时,我将当前时间和开始时间之间的小时差转换为准确的时间,以便稍后存储在 map 中。 Like this:像这样:

        int latestHour = (int) diffInHours;
        while (latestHour >= openStoreDateTime.toInstant().atZone(ZoneId.systemDefault()).getHour()) {
            Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone(ZoneId.systemDefault()));
            String hourConversion;
            if (closeStoreDateTime.toInstant().atZone(ZoneId.systemDefault()).getDayOfYear() > openStoreDateTime.toInstant().atZone(ZoneId.systemDefault()).getDayOfYear()) {
                if (latestHour > 24) {
                    calendar.add(Calendar.HOUR_OF_DAY, latestHour);
                    hourConversion = calendar.get(Calendar.AM_PM) == Calendar.AM ? String.valueOf(calendar.get(Calendar.HOUR)) : String.valueOf(calendar.get(Calendar.HOUR) + 12);
                } else {
                    calendar.add(Calendar.HOUR_OF_DAY, latestHour);
                    int AM_PM = calendar.get(Calendar.AM_PM);
                    if (AM_PM == Calendar.AM || (AM_PM == Calendar.PM && calendar.get(Calendar.HOUR) == 12)) {
                        hourConversion = String.valueOf(calendar.get(Calendar.HOUR));
                    } else {
                        hourConversion = String.valueOf(calendar.get(Calendar.HOUR) + 12);
                    }
                }
            } else {
                hourConversion = String.valueOf(latestHour);
            }

This works if the dates are within the same year, but does not work if they are in different years because the day in the begin year (363) is greater than the day in the end year.如果日期在同一年内,则此方法有效,但如果它们在不同年份,则无效,因为开始年份 (363) 中的日期大于结束年份中的日期。 Does anyone have an idea how to convert hours between 2 different dates into a useable date?有谁知道如何将 2 个不同日期之间的小时数转换为可用日期? Thank you.谢谢你。

If I understand your question correctly, you want a List of date-time values from an end date-time through a start date-time, one for each hour.如果我正确理解您的问题,您需要从结束日期时间到开始日期时间的日期时间值List ,每小时一个。

Here are some test results.以下是一些测试结果。

2021-02-11T16:00
2021-02-11T15:00
2021-02-11T14:00
2021-02-11T13:00
2021-02-11T12:00
...
2020-12-30T12:00
2020-12-30T11:00
2020-12-30T10:00
2020-12-30T09:00
2020-12-30T08:00

Here's runnable code that will do that using the LocalDateTime class.这是使用LocalDateTime class 的可运行代码。 You can display a LocalDateTime value anyway you want using a DateTimeFormatter .您可以使用DateTimeFormatter以任何方式显示LocalDateTime值。

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class DateIntervals {

    public static void main(String[] args) {
        String endDateString = "2021-02-11 16:00:00";
        String startDateString = "2020-12-30 08:00:00";
        
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
                "yyyy-MM-dd HH:mm:ss");
        LocalDateTime endDate = LocalDateTime.parse(endDateString, formatter);
        LocalDateTime startDate = LocalDateTime.parse(startDateString, formatter);
        
        List<LocalDateTime> intervals = new ArrayList<>();
        LocalDateTime date = endDate;
        while (date.isAfter(startDate)) {
            intervals.add(date);
            date = date.minusHours(1L);
        }
        intervals.add(date);
        
        for (int i = 0; i < intervals.size(); i++) {
            System.out.println(intervals.get(i));
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM