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);
In UTC:
int hour = (int)(date.getTime() % 86400000) / 3600000;
or
long hour = (date.getTime() % 86400000) / 3600000;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With