如何解决获取当前日期中的下一个特定LocalDateTime或返回下一个特定的LocalDateTime
我想在下一个(例如)5:00 AM
的日期/时间创建LocalDateTime的实例。
要求:
- 当前LocalDateTime>
2020-08-14T10:57:45.592035
,需要返回2020-08-15T05:00:00.000000
- 当前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#isBefore
和LocalDateTime#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 举报,一经查实,本站将立刻删除。