打印 Java 中两个时间戳之间的日期时间

Print Datetime between Two timestamps in Java

这是我的要求。很确定有一种优雅的方法可以做到这一点。需要你的帮助。我需要构建一个后端程序来处理多个日期并将其插入 table 使用用户选择的开始和结束时间。

输入:

StartTime: 11/26/2021 11:00 PM
EndTime: 11/28/2021 04:00 AM

我期待的输出

11/26/2021 11:00 PM
11/26/2021 11:59 PM
11/27/2021 12:00 AM
11/27/2021 11:59 PM
11/28/2021 12:00 AM
11/28/2021 04:00 AM

我的代码

public static void main(String[] args) {
        
        String StartTime = "11/27/2021 11:00 PM";
        String EndTime = "11/29/2021 04:00 AM";
        
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm a");
        LocalDate localStartDate = LocalDate.parse(StartTime, formatter);
        LocalDate localEndDate = LocalDate.parse(EndTime, formatter);

        LocalDateTime  localStartTime = LocalDateTime.parse(StartTime,formatter);
        LocalDateTime  localEndTime = LocalDateTime.parse(EndTime,formatter);
        
        
        List<LocalDateTime> totalDates = new ArrayList<>();
        while (!localStartTime.isAfter(localEndTime)) {
            totalDates.add(localStartTime);
            totalDates.add(localStartTime.with(LocalTime.MAX));
            localStartTime = localStartTime.plusDays(1);
            localStartTime = localStartTime.with(LocalTime.MIN);
        }
        
        totalDates.add(localEndTime.with(LocalTime.MIN));
        totalDates.add(localEndTime);
        
        for (int i = 0; i < totalDates.size(); i++) {
            System.out.print(totalDates.get(i) + "\n");
        }

    }

我的输出:

2021-11-27T23:00
2021-11-27T23:59:59.999999999
2021-11-28T00:00
2021-11-28T23:59:59.999999999
2021-11-29T00:00
2021-11-29T23:59:59.999999999
2021-11-29T00:00
2021-11-29T04:00

您还需要解析输出。因此,您可以使用已经定义的 DateTimeFormatter,它有一个 parse 方法。

输出代码:

System.out.println(totalDates.get(i).format(formatter));

您的输出中还有一行。这个来自你设置localStartTime = localStartTime.with(LocalTime.MIN);的行。在这里,我建议进行此更改:

LocalDateTime workingStartTime = localStartTime;

List<LocalDateTime> totalDates = new ArrayList<>();
while (!workingStartTime.isAfter(localEndTime)) {
  if(!workingStartTime.equals(localStartTime)) {
    workingStartTime = workingStartTime.with(LocalTime.MIN);
  }
  totalDates.add(workingStartTime);
  totalDates.add(workingStartTime.with(LocalTime.MAX));
  workingStartTime = workingStartTime.plusDays(1);
}

编辑:使用流和 flatMap 的解决方案

以下是使用流构建最终列表的方法(不需要额外添加最后一天):

List<LocalDateTime> totalDates = localStartTime.toLocalDate().datesUntil(localEndTime.toLocalDate().plusDays(1))
    .flatMap(date -> Stream.of(
        date.equals(localStartTime.toLocalDate()) ? LocalDateTime.of(date, localStartTime.toLocalTime()) : LocalDateTime.of(date, LocalTime.MIN),
        date.equals(localEndTime.toLocalDate()) ? LocalDateTime.of(date, localEndTime.toLocalTime()) : LocalDateTime.of(date, LocalTime.MAX))
    )
    .collect(Collectors.toList());
String startTime = "11/26/2021 11:00 PM";
String endTime = "11/28/2021 04:00 AM";

DateTimeFormatter dtf =
        DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm a");
LocalDateTime start = LocalDateTime.parse(startTime, dtf);
LocalDateTime end = LocalDateTime.parse(endTime, dtf);
  • 开始播放给定的日期
  • 然后通过使用当前 LocalDate 实例创建两个 LocalDateTime 实例来创建内部流。
    • 当前日期时间 1 minute 午夜前
    • 当前日期时间 1 day 午夜前
  • 然后 flatMap 流式传输并将日期时间放入链表中。
  • 要完成,请将当前开始 LocalDateTime 添加到列表的开头
  • 和结尾LocalDateTime到列表末尾
List<LocalDateTime> list =
        start.toLocalDate().datesUntil(end.toLocalDate())
                .flatMap(date -> Stream.of(
                        date.atTime(23,59),
                        date.atTime(0,0).plusDays(1)))
                .collect(Collectors
                        .toCollection(LinkedList::new));
list.add(0, start);
list.add(end);
list.forEach(d -> System.out.println(d.format(dtf)));

打印

11/26/2021 11:00 PM
11/26/2021 11:59 PM
11/27/2021 12:00 AM
11/27/2021 11:59 PM
11/28/2021 12:00 AM
11/28/2021 04:00 AM

这似乎有效。如果还有其他优雅的方法可以做到这一点..请告诉我。

public static void main(String[] args) {

    String StartTime = "02/27/2021 11:00 PM";
    String EndTime = "03/02/2021 01:00 PM";

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm a");
    LocalDate localStartDate = LocalDate.parse(StartTime, formatter);
    LocalDate localEndDate = LocalDate.parse(EndTime, formatter);

    LocalDateTime localStartTime = LocalDateTime.parse(StartTime, formatter);
    LocalDateTime localEndTime = LocalDateTime.parse(EndTime, formatter);

    List<LocalDateTime> totalDates = new ArrayList<>();
    while (localEndDate.isAfter(localStartDate)) {

        totalDates.add(localStartTime);
        totalDates.add(localStartTime.with(LocalTime.MAX));

        if (!localStartDate.equals(localEndDate)) {
            localStartTime = localStartTime.plusDays(1).with(LocalTime.MIN);
        }
        localStartDate = localStartDate.plusDays(1);

    }

    totalDates.add(localEndTime.with(LocalTime.MIN));
    totalDates.add(localEndTime);

    for (int i = 0; i < totalDates.size(); i++) {
        System.out.print(formatter.format(totalDates.get(i)) + "\n");
    }
}