简体   繁体   中英

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.

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. 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. Does anyone have an idea how to convert hours between 2 different dates into a useable date? 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.

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. You can display a LocalDateTime value anyway you want using a DateTimeFormatter .

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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