1, Problem Description:
I get the maximum time of the day:
Public Static Date Getendofday (date date) { = localdatetime.ofinstant (Instant.ofepochmilli (Date.gettime ()), Zoneid.systemdefault ());; = Localdatetime.with (Localtime.max); return Date.from (Endofday.atzone (Zoneid.systemdefault ()). Toinstant ());
Gets the maximum time to insert into the database, always one second, for example, the current time is 2018-04-26 22:00:01 the maximum time obtained is: 2018-04-26 23:59:59 Here you can see the source code for hours:
To set Max's source code:
Static { for (int i = 0; i < hours.length; i++) { new localtime (i, 0, 0, 0); } = Hours[0]; = Hours[12]; = Hours[0]; New LocalTime (,999_999_999); }
After inserting into the database became 2018-04-27 00:00:00, it was strange to add a second, after checking the data found that:MySQL database for milliseconds greater than 500 of data will be carry!!!
2. Solutions
I changed the last millisecond to 0, and by looking at the source code of the Java time class, I found that it was set time,
/*** Obtains an instance of {@codelocaltime} from an hour, minute, second and nanosecond. * <p> * This returns a {@codeLocalTime} with the specified hour, minute, second and nanosecond. * * @paramHour The Hour-of-day to represent, from 0 to *@paramminute the Minute-of-hour to represent, from 0 to *@paramsecond The Second-of-minute to represent, from 0 to *@paramNanoofsecond The Nano-of-second to represent, from 0 to 999,999,999 *@returnThe local time, NOT NULL *@throwsdatetimeexception If the value of any field was out of range*/ Public StaticLocalTime of (intHourintMinuteintSecondintNanoofsecond) {Hour_of_day.checkvalidvalue (HOUR); Minute_of_hour.checkvalidvalue (MINUTE); Second_of_minute.checkvalidvalue (SECOND); Nano_of_second.checkvalidvalue (Nanoofsecond); returnCreate (hour, minute, second, nanoofsecond); }
So I modified the code to get the maximum time of the day:
/** * get the maximum time of the day 23:59:59 * New LocalTime (23, 59, 59, 999_999_999) * is not set to the maximum value since MySQL (some versions) will have a carry operation with a millisecond value greater than 500 for the inserted time after setting the maximum value, so the millisecond value is set to 0. * @param date * @return */ public static date getendofday (date date) {LocalDateTime Localdat ETime = Localdatetime.ofinstant (Instant.ofepochmilli (Date.gettime ()), Zoneid.systemdefault ());; LocalDateTime endofday = Localdatetime.with (Localtime.of (23,59,59,0 return Date.from (Endofday.atzone ( Zoneid.systemdefault ()). Toinstant ()); }
Check it out, OK.
Reference: https://my.oschina.net/u/2353881/blog/1573811
MySQL insertion time inexplicable plus a second