我希望在10分钟内缩小当前时间。我可以在Java,php或mysql(最好)上这样做,无论什么被认为更好。例如:
08:49:50 -> 08:40:00
14:23:12 -> 14:20:00
09:32:11 -> 09:30:00
12:20:00 -> 12:20:00
我尝试从Round date to 10 minutes interval解决问题,但mysql失败了。
答案 0 :(得分:1)
以下是stackoverflow帖子的回答:How to round a time to the nearest 15 minute segment
SELECT FROM_UNIXTIME( TRUNCATE(UNIX_TIMESTAMP(NOW()) / 900,0)*900);
致Gavin Towey的信用!
用600(10分钟)替换900(15分钟)。
答案 1 :(得分:0)
public static Date roundDown10Min(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int minutes = cal.get(Calendar.MINUTE);
int roundedMinutes = (minutes/10)*10;
cal.set(Calendar.MINUTE, roundedMinutes );
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}