You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于给定DateTime时间区间,如何按小时拆分并输出每个时间点?

Got it, let's get this time interval splitting sorted out properly. Your existing code already has the basics covered—splitting the interval string and parsing the start/end times. Here's the full, working implementation to output every hourly time point in your range, plus some key notes:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class TimeSplitter {
    public static void main(String[] args) {
        String TimeInterval = "2018-05-03T04:00Z/2018-05-03T06:00Z";
        
        // Split the input interval into start and end time strings
        String[] All_Time = TimeInterval.split("/");
        
        // Define the formatter that matches your input's date-time pattern
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm'Z'");
        
        // Parse the start and end times into LocalDateTime objects
        LocalDateTime datetimestart = LocalDateTime.parse(All_Time[0], dateFormat);
        LocalDateTime datetimeend = LocalDateTime.parse(All_Time[1], dateFormat);
        
        // Use a do-while loop to ensure we always process the start time first
        do {
            // Format the time back to your desired string format (instead of using toString())
            String formattedTime = datetimestart.format(dateFormat);
            System.out.println(formattedTime);
            
            // Increment the current time by 1 hour—Java's time API handles this cleanly
            datetimestart = datetimestart.plusHours(1);
        } while (!datetimestart.isAfter(datetimeend)); // Stop once we pass the end time
    }
}

Key Details & Improvements:

  • Hourly Increment: We use plusHours(1) to safely increment the time by one hour every iteration. This is far more reliable than manually manipulating date components (no worries about edge cases like day/month rollovers).
  • Inclusive End Time: The loop condition !datetimestart.isAfter(datetimeend) ensures we include the end time (2018-05-03T06:00Z) in our output. If you wanted to exclude the end time, you'd change this to datetimestart.isBefore(datetimeend).
  • Proper Formatting: Instead of using datetimestart.toString() (which would output something like 2018-05-03T04:00 without the Z), we use the same DateTimeFormatter to format the output back to your original desired format.

Quick Note on Time Zones:

Your input times include a Z (indicating UTC), but LocalDateTime ignores timezone information. If you need to handle time zones correctly (to avoid issues with local timezone offsets), switch to ZonedDateTime instead—it can parse the ISO 8601 format directly without a custom formatter:

// Replace LocalDateTime with ZonedDateTime for timezone-aware handling
ZonedDateTime datetimestart = ZonedDateTime.parse(All_Time[0]);
ZonedDateTime datetimeend = ZonedDateTime.parse(All_Time[1]);

This will preserve the UTC timezone and prevent any unintended shifts from your local system time.

内容的提问来源于stack exchange,提问作者aPugLife

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:12:08