英文:
java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Seconds
问题
我想要获取两个日期时间值之间的分钟差。
public long datetimeDiffInMinutes(String dateStop, String dateStart) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDate firstDate = LocalDate.parse(dateStart, formatter);
LocalDate secondDate = LocalDate.parse(dateStop, formatter);
Duration d1 = Duration.between(firstDate, secondDate);
long min = d1.toMinutes();
return min;
}
会抛出异常:java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Seconds
但是在这个函数中我没有使用 "Seconds"。这行代码抛出了异常:Duration d1 = Duration.between(firstDate, secondDate);
英文:
i like to get the duration between to datetime values in minutes.
public long datetimeDiffInMinutes(String dateStop, String dateStart) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDate firstDate = LocalDate.parse(dateStart, formatter);
LocalDate secondDate = LocalDate.parse(dateStop, formatter);
Duration d1 = Duration.between(firstDate, secondDate);
long min = d1.toMinutes();
return min;
}
There will be thrown an exception: java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Seconds
But i dont use "Seconds" in this function. This line throws the exception: Duration d1 = Duration.between(firstDate, secondDate);
答案1
得分: 3
关于你正在调用的方法 Duration.between(Temporal, Temporal)
的文档说明如下:
指定的时间对象必须支持
SECONDS
单位。为了完全准确,应支持NANOS
单位或NANO_OF_SECOND
字段。
但 LocalDate.isSupported
的文档中写道:
如果单位是 ChronoUnit,则查询在此处实现。支持的单位包括:DAYS, WEEKS, MONTHS, YEARS, DECADES, CENTURIES, MILLENNIA, ERAS
所有其他 ChronoUnit 实例都将返回 false。
因此,LocalDate
不支持秒,而这是你所调用的方法所需的。
值得考虑的是,Duration
旨在表示 经过的 时间 - 固定的秒数等。两个日期之间经过的时间可能取决于涉及的时区 - 因为涉及时区时,一天并不总是有24小时。
如果你愿意 假定 一天有24小时,你可以使用 Duration.ofDays(DAYS.between(firstDate, secondDate))
。
英文:
The documentation for the method you're calling (Duration.between(Temporal, Temporal)
) states:
> The specified temporal objects must support the SECONDS
unit. For full accuracy, either the NANOS
unit or the NANO_OF_SECOND
field should be supported.
But LocalDate.isSupported
is documented with:
> If the unit is a ChronoUnit then the query is implemented here. The supported units are: DAYS, WEEKS, MONTHS, YEARS, DECADES, CENTURIES, MILLENNIA, ERAS
>
> All other ChronoUnit instances will return false.
So no, LocalDate
doesn't support seconds, which is required for the method you're calling.
It may be worth considering that a Duration
is intended to be an elapsed time - a fixed number of seconds etc. The elapsed time between two dates may depend on the time zone involved - because a day doesn't always have 24 hours when there are time zones involved.
If you're happy assuming a 24-hour day, you could use Duration.ofDays(DAYS.between(firstDate, secondDate))
.
答案2
得分: 3
您使用时间信息来指定日期,这使得LocalDate
不是一个最佳选择。LocalDateTime
是更好的选项。这已经允许您创建持续时间。
由于夏令时的影响,结果可能会有偏差。添加正确的时区信息应该解决这个问题:
ZoneId zone = ZoneId.systemDefault(); // 或者指定一个时区
ZonedDateTime firstDateTime = LocalDateTime.parse(dateStart, formatter).atZone(zone);
ZonedDateTime secondDateTime = LocalDateTime.parse(dateStop, formatter).atZone(zone);
Duration d1 = Duration.between(firstDateTime, secondDateTime);
long min = d1.toMinutes();
英文:
You specify your dates with time information. That makes LocalDate
a suboptimal choice. LocalDateTime
is a better option. That already lets you create a duration.
Results may be off because of DST. Adding the right time zone should solve that:
ZoneId zone = ZoneId.systemDefault(); // or an explicit one
ZonedDateTime firstDateTime = LocalDateTime.parse(dateStart, formatter).atZone(zone);
ZonedDateTime secondDateTime = LocalDateTime.parse(dateStop, formatter).atZone(zone);
Duration d1 = Duration.between(firstDateTime, secondDateTime);
long min = d1.toMinutes();
答案3
得分: 1
关于日期之间的差异,Period
是更好的表示方式。
然而,由于您的格式字符串包含时间,看起来您想要解析到LocalDateTime
而不是LocalDate
。这样,您关心的分钟(和秒)不会被丢弃:
private static final DateTimeFormatter parser =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public long datetimeDiffInMinutes(String dateStop, String dateStart) {
LocalDateTime firstDate = parser.parse(dateStart, LocalDateTime::from);
LocalDateTime secondDate = parser.parse(dateStop, LocalDateTime::from);
return firstDate.until(secondDate, ChronoUnit.MINUTES);
}
请注意,因为您没有关于时区和可能发生的夏令时转换的信息,结果可能不会在每种情况下与人们的期望相匹配。如果需要的话,您应该澄清用例,并获取有关时区的更多信息。
英文:
For differences between dates, Period
is a better representation.
Because your format string contains time, however, it looks like you want to be parsing to LocalDateTime
instead of LocalDate
. This way, the minutes (and seconds) you care about are not discarded:
private static final DateTimeFormatter parser =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public long datetimeDiffInMinutes(String dateStop, String dateStart) {
LocalDateTime firstDate = parser.parse(dateStart, LocalDateTime::from);
LocalDateTime secondDate = parser.parse(dateStop, LocalDateTime::from);
return firstDate.until(secondDate, ChronoUnit.MINUTES);
}
Note that because you don't have information about the time zone and any daylight-saving transitions that may occur, the results might not match what people expect in every case. You should clarify the use case, and get more information about the zone if necessary.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论