我试图获得两个本地日期之间的时间。我使用了下面的代码:
LocalDate startDate = LocalDate.of(2000, 4, 30);
long noOfHours = startDate.until(LocalTime.now(), ChronoUnit.HOURS);
System.out.println(noOfHours);
Run Code Online (Sandbox Code Playgroud)
我有以下错误:
Exception in thread "main" java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: 13:44:53.095819600 of type java.time.LocalTime
at java.base/java.time.LocalDate.from(LocalDate.java:397)
at java.base/java.time.LocalDate.until(LocalDate.java:1645)
at BirthDay.App.main(App.java:36)
Run Code Online (Sandbox Code Playgroud) 我需要从a获取日期String,格式如下:uuuu-MM-dd HH:mm
输入具有该格式,我希望我的日期格式相同(年 - 月 - 日小时:分钟).当我这样做时:
LocalDate aux = LocalDate.parse(times.get(index),DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm "));
Run Code Online (Sandbox Code Playgroud)
有了这个输入:2017-12-05 20:16
我只得到这个:2017-12-05
我不知道为什么,我尝试了很多格式,总是丢失时间和分钟.任何的想法?
我怎么知道两个时间表之间是否有一个小时?
例:
10:00 is between 8:00 and 12:00?
7:30 is between 8:00 and 12:00?
Run Code Online (Sandbox Code Playgroud)
我正在尝试这种方式:
LocalDateTime s = LocalDateTime.of (2017, 10, 20, 8, 00);
LocalDateTime f = LocalDateTime.of (2017, 10, 20, 12, 00);
LocalDate test = LocalDate.of (2017, 10, 20, 10, 00); (<- Must be LocalDate)
if ((test.isAfter (s)) && (test.isBefore (s))
return true;
else
return false;
Run Code Online (Sandbox Code Playgroud)
它返回以下错误:
方法isBefore(ChronoLocalDateTime)在类型LocalDateTime中不适用于参数(LocalTime)
考虑下面的代码
Instant instant = Instant.now();
System.out.println(instant);
Run Code Online (Sandbox Code Playgroud)
结果:
2020-01-13T09:01:06.405Z
Run Code Online (Sandbox Code Playgroud)
现在从上面的结果我想得到当前的小时和当前的分钟。
使用新的Java 8 DateTime API(java.time),我们如何检查"最后"捕获时间是否早于配置的秒集?
例...
上次拍摄时间:13:00:00当前时间:13:00:31
if (last captured time is older then 30 seconds) then
do something
Run Code Online (Sandbox Code Playgroud) 如何使用Java 8的java.timeAPI 确定日期是否在两个其他日期之间?