Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to get hour of java.util.date?

Tags:

java

date

When starting from ajava.util.date object: what is the best way getting the hour part as an integer regarding performance?

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);
like image 901
membersound Avatar asked Jul 05 '16 09:07

membersound


1 Answers

In UTC:

int hour = (int)(date.getTime() % 86400000) / 3600000;

or

 long hour = (date.getTime() % 86400000) / 3600000;
like image 173
Rustam Avatar answered Nov 11 '22 16:11

Rustam