Java Milliseconds in Year

前端 未结 9 1919
终归单人心
终归单人心 2020-12-09 08:24

I am doing some date calculations in Java using milliseconds and noticing an issue with the following:

private static final int MILLIS_IN_SECOND = 1000;
             


        
9条回答
  •  清歌不尽
    2020-12-09 08:46

    private static final long MILLISECONDS_IN_YEAR = MILLIS_IN_SECOND * ...
    

    All the operands on the right hand side are ints, so the multiplication is done with 32bit signed integers, which overflows. Cast the first one to long and you'll get the expected value.

    private static final long MILLISECONDS_IN_YEAR = (long)MILLIS_IN_SECOND * ...
    

提交回复
热议问题