[英]Fastest way to get hour of java.util.date?
When starting from a java.util.date
object: what is the best way getting the hour part as an integer
regarding performance? 从
java.util.date
对象开始时:将小时部分作为integer
关于性能的最佳方法是什么?
I have to iterate a few million dates, thus performance matters. 我必须迭代几百万个日期,因此性能很重要。
Normally I'd get the hour as follows, but maybe there are better ways? 通常我会得到如下的时间,但也许有更好的方法?
java.util.Date date;
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int hours = calendar.get(Calendar.HOUR_OF_DAY);
In UTC: 在UTC中:
int hour = (int)(date.getTime() % 86400000) / 3600000;
or 要么
long hour = (date.getTime() % 86400000) / 3600000;
Date dateInput = new Date();
since calendar starts at 01.01.1970, 01:00
. 因为日历从
01.01.1970, 01:00
开始。 you have to make further modifications to the code.using below approach avoids that so this will performs faster. 您必须对代码进行进一步修改。使用以下方法可以避免这种情况,因此这将更快地执行。
dateInput.toInstant().atZone(ZoneId.systemDefault()).getHour();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.