Java Calendar adds a random number of milliseconds?

后端 未结 4 898
栀梦
栀梦 2021-01-21 14:59

Hi I have something weird happening. I am simply taking a calendar object, converting it to its individual parts, and putting it back into a calendar (with or without any change

4条回答
  •  半阙折子戏
    2021-01-21 15:50

    When you initialize the Calendar object, it is getting the current time including the current second and millisecond. The code provided sets the hour and minute, but it does not set the second and millisecond, leaving it as it was when the Calendar object was initialized. In order to set the second and nanosecond to zero, use:

    StartCalendar.set(Year, Month, DayofMonth, Hour, Minute, 0,0);
    

提交回复
热议问题