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

后端 未结 2 1089
隐瞒了意图╮
隐瞒了意图╮ 2021-01-18 05:30

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

I have to iterat

相关标签:
2条回答
  • 2021-01-18 05:57

    In UTC:

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

    or

     long hour = (date.getTime() % 86400000) / 3600000;
    
    0 讨论(0)
  • 2021-01-18 05:59
    Date dateInput = new Date();
    

    since calendar starts at 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();
    
    0 讨论(0)
提交回复
热议问题