我獲取當天最大的時間:結果是當天的23:59:59秒mysql
1 public static Date getDateWithMaxTime(Date date) { 2 Calendar calendar = Calendar.getInstance(); 3 calendar.setTime(date); 4 calendar.set(Calendar.HOUR_OF_DAY, 23); 5 calendar.set(Calendar.MINUTE, 59); 6 calendar.set(Calendar.SECOND, 59); 7 return calendar.getTime(); 8 }
可是插入數據庫的時間多了1秒鐘,變成次日的00:00:00sql
我把最後一位毫秒變爲0 了,mysql(有些版本的)會對插入的時間的毫秒值大於500的進位操做,因此在此地設置毫秒值爲0.數據庫
1 public static Date getDateWithMaxTime(Date date) { 2 Calendar calendar = Calendar.getInstance(); 3 calendar.setTime(date); 4 calendar.set(Calendar.HOUR_OF_DAY, 23); 5 calendar.set(Calendar.MINUTE, 59); 6 calendar.set(Calendar.SECOND, 59); 7 calendar.set(Calendar.MILLISECOND,0); //增長毫秒設置爲0 8 return calendar.getTime(); 9 }