Back to project page timesync.
The source code is released under:
Apache License
If you think the Android project timesync listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package me.tatarka.timesync.lib; //from w w w . j a v a 2s . c o m import java.util.Calendar; import java.util.GregorianCalendar; import static me.tatarka.timesync.lib.MathUtil.divCeil; class EventCalculator { /** * Calculate when to fire the next event. This should happen at the next timestamp which is a * multiple of the given interval since midnight in the current timezone. Both the arguments and * the result are in unix time milliseconds (equivalent to {@link System#currentTimeMillis()}). * * @param currentTime the time to start from, the result will be the next event after this time * @param interval the interval * @return the next event time. */ public static long getNextEvent(long currentTime, long interval) { if (interval == 0) return currentTime; long startTime = getPreviousMidnight(currentTime); long span = currentTime - startTime; long result; if (span == 0) { // It's midnight! result = startTime + interval; } else { result = startTime + divCeil(span, interval) * interval; } return result; } private static long getPreviousMidnight(long currentTime) { Calendar date = new GregorianCalendar(); date.setTimeInMillis(currentTime); date.set(Calendar.HOUR_OF_DAY, 0); date.set(Calendar.MINUTE, 0); date.set(Calendar.SECOND, 0); date.set(Calendar.MILLISECOND, 0); return date.getTimeInMillis(); } }