我需要快速实现"日期跳转"算法,该算法将包含在事件管理系统中.
触发事件并将日期(以同步方法)设置为下一个第10分钟.
例如
Event occurs at "2010-01-05 13:10:12" and sets the
next date to be "2010-01-05 13:20:00"
Run Code Online (Sandbox Code Playgroud)
如果事件恰好(假设)在第10分钟发生,则必须设置下一个事件
Event occurs at "2010-01-05 13:30:00" and sets the
next date to be "2010-01-05 13:40:00"
Run Code Online (Sandbox Code Playgroud)
(不太可能,因为日期下降到1/1000秒,但以防万一......).
我的第一个想法是获取当前Date()并直接使用msgetTime()方法,通过整数(长)除法,如((time / 10mn)+1)*10mn.
既然它必须快速且可靠,我想我会在实施之前询问我的同事OSers.
你可以使用/调整我的答案来解决一个非常相似的问题:
像这样的东西:
int unroundedMinutes = calendar.get(Calendar.MINUTE);
int mod = unroundedMinutes % 10;
calendar.add(Calendar.MINUTE, mod == 0 ? 10 : 10 - mod);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Run Code Online (Sandbox Code Playgroud)