微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

获取当前日期中的下一个特定LocalDateTime或返回下一个特定的LocalDateTime

如何解决获取当前日期中的下一个特定LocalDateTime或返回下一个特定的LocalDateTime

我想在下一个(例如)5:00 AM的日期/时间创建LocalDateTime的实例。

要求:

  1. 当前LocalDateTime> 2020-08-14T10:57:45.592035,需要返回2020-08-15T05:00:00.000000
  2. 当前LocalDateTime> 2020-08-14T02:57:45.592035,需要返回2020-08-14T05:00:00.000000

Java Time API中是否有任何方法,还是应该手动进行计算,例如在当前日期中的Default set 5:00 AM并与return(set)date进行比较,如果小于,则将其与当前日期相加,然后加上plus days(1)和5 :00 AM ELSE返回设置日期?

引用:dateTime.with(TemporalAdjusters.next(DayOfWeek.MONDAY)) //但是它从今天(当前日期)的下周一返回。

获得手动解决方案>

LocalDateTime currentDateTimeInLosAngeles = LocalDateTime.Now(ZoneId.of(timeZone));
        LocalDateTime nextTimeAt530 = null;

        System.out.println(currentDateTimeInLosAngeles);

        if (currentDateTimeInLosAngeles.toLocalTime().isAfter(LocalTime.of(5,30,0))) {

            System.out.println("Next Day at 5:30");

            nextTimeAt530 = LocalDateTime.of(LocalDate.Now(ZoneId.of(timeZone)),LocalTime.of(5,0))
                    .plusDays(1);
            System.out.println(nextTimeAt530);

        } else {

            System.out.println("Current Day at 5:30");

            nextTimeAt530 = LocalDateTime.of(LocalDate.Now(ZoneId.of(timeZone)),0));
            System.out.println(nextTimeAt530);
        }

        System.out.println("\n -- FOR MAIN DIFFERENCE");

        System.out.println(currentDateTimeInLosAngeles);
        System.out.println(nextTimeAt530);

解决方法

您可以执行以下操作:

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

public class Main {
    public static void main(String[] args) {
        // Given date-time string
        String strDateTime = "2020-08-14T10:57:45.592035";

        // Get LocalDateTime by parsing the date-time string
        LocalDateTime ldt = LocalDateTime.parse(strDateTime);

        // Get a new LocalDateTime instance with the time as 5:00:00:00.0 
        // in the parsed LocalDateTime
        ldt = ldt.withHour(5)
                .withMinute(0)
                .withSecond(0)
                .withNano(0);
        System.out.println(ldt);

        // Define the format to get the date-time string representation of LocalDateTime
        // in the desired pattern
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSSSS");

        // Get string representation of LocalDateTime in the desired pattern
        String formattedDateTimeStr = formatter.format(ldt);
        System.out.println(formattedDateTimeStr);

        // Add one day
        ldt = ldt.plusDays(1);
        System.out.println(ldt);

        // Get string representation of LocalDateTime in the desired pattern
        formattedDateTimeStr = formatter.format(ldt);
        System.out.println(formattedDateTimeStr);
    }
}

输出:

2020-08-14T05:00
2020-08-14T05:00:00.000000
2020-08-15T05:00
2020-08-15T05:00:00.000000

LocalDateTime#isBeforeLocalDateTime#isAfter

使用这些方法检查日期时间是否在另一个时间之前/之后。

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

public class Main {
    public static void main(String[] args) {
        // Given date-time string
        String strDateTime = "2020-08-14T02:57:45.592035";

        // Get LocalDateTime by parsing the date-time string
        LocalDateTime ldtGiven = LocalDateTime.parse(strDateTime);
        System.out.println(ldtGiven);

        // Get a new LocalDateTime instance with the time as 5:00:00:00.0 
        // in the parsed LocalDateTime
        LocalDateTime ldtGivenWith5AM = ldtGiven.withHour(5)
                                    .withMinute(0)
                                    .withSecond(0)
                                    .withNano(0);

        // Check if the time is before 5:00
        LocalDateTime ldtNew = null;
        if (ldtGiven.isBefore(ldtGivenWith5AM)) {
            ldtNew = ldtGivenWith5AM.plusDays(1);
        }
        System.out.println(ldtNew);

        // Define the format to get the date-time string representation of LocalDateTime
        // in the desired pattern
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSSSS");

        // Get string representation of LocalDateTime in the desired pattern
        String formattedDateTimeStr = formatter.format(ldtNew);
        System.out.println(formattedDateTimeStr);
    }
}

输出:

2020-08-14T02:57:45.592035
2020-08-15T05:00
2020-08-15T05:00:00.000000

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?