[英]Is resultant java.time code has more code statements compared to equivalent Calendar code
Recently, I try to port one of our old code base, from Calendar
to java.time
, as we need quite a number of arithmetic functionalities, which is only found in java.time
. 最近,我尝试将我们的旧代码库之一从
Calendar
java.time
到java.time
,因为我们需要大量的算术功能,这些功能只能在java.time
找到。
If we use Calendar
in our current code base, we need to perform a lot of conversion back-and-forth (From Calendar
to Instant
, From Instant
back to Calendar
), in the middle of our code. 如果我们在当前代码库中使用
Calendar
,则需要在代码中间来回执行很多转换(从Calendar
到Instant
,从Instant
到Calendar
)。
To avoid such cumbersome conversion, we decide to eliminate usage of Calendar
, and port them to equivalent java.time
code. 为了避免这种麻烦的转换,我们决定消除
Calendar
使用,并将它们移植到等效的java.time
代码。
I'm a bit skeptical on my port. 我对港口有点怀疑。 As compared with Calendar code, it seems to
与日历代码相比,似乎
// reminderCal is Calendar object.
long startTimestamp = getStartTimestamp();
reminderCal.setTimeInMillis(startTimestamp);
while (startTimestamp <= maxTimestamp) {
resultList.add(startTimestamp);
reminderCal.add(Calendar.DAY_OF_MONTH, 1);
startTimestamp = reminderCal.getTimeInMillis();
}
return resultList;
// Epoch timestamp loopTs as initial input.
long startTimestamp = getStartTimestamp();
final ZoneId zoneId = ZoneId.systemDefault();
while (startTimestamp <= maxTimestamp) {
resultList.add(startTimestamp);
// More code, more temporary instances required compared
// with Calendar's version. Not sure we're doing the right
// way.
Instant instant = Instant.ofEpochMilli(startTimestamp);
LocalDateTime time = LocalDateTime.ofInstant(instant, zoneId);
time = time.plus(1, ChronoUnit.DAYS);
startTimestamp = time.atZone(zoneId).toInstant().toEpochMilli();
}
return resultList;
For the above code, I was wondering, are we doing the port correctly and optimized? 对于上面的代码,我想知道,我们是否正确地进行了端口优化? Is there any room we can improve in our
java.time
's port? 在
java.time
的端口中,我们还有什么地方可以改善?
Since you want date manipulations between times in a given time zone, you shouldn't use milliseconds nor LocalDateTime, but ZonedDateTime. 由于您希望在给定时区中的时间之间进行日期操作,因此您不应使用毫秒或LocalDateTime,而应使用ZonedDateTime。 And I would argue that your List should contain Instants instead of longs, but let's keep it that way for now:
我认为您的列表应该包含Instants而不是longs,但是现在让我们保持这种状态:
long startTimestamp = getStartTimestamp();
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime maxDateTime = Instant.ofEpochMilli(maxTimestamp).atZone(zoneId);
ZonedDateTime loopDateTime = Instant.ofEpochMilli(loopTs).atZone(zoneId);
while (!loopDateTime.isAfter(maxDateTime)) {
tsList.add(loopDateTime.toInstant().toEpochMilli());
loopDateTime = loopDateTime.plusDays(1);
}
This is more concise, but also more readable. 这更简洁,但也更具可读性。 And all the
Instant.ofEpochMilli()
and toEpochMilli()
calls wouldn't be needed if you wroked with Instants instead of longs. 如果您使用Instants而不是long进行操作,则不需要所有
Instant.ofEpochMilli()
和toEpochMilli()
调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.