GetCurrentRealTime
suggest changeThis calculates the current time from a trusted reading. Set networkTime and elapsedRealtimeAtSample together when that reading is taken.
private static long networkTime;
private static long elapsedRealtimeAtSample;
public static Calendar getCurrentRealTime() {
long currentRealTime = networkTime
+ (SystemClock.elapsedRealtime() - elapsedRealtimeAtSample);
Calendar calInstance = Calendar.getInstance();
calInstance.setTimeZone(getUTCTimeZone());
calInstance.setTimeInMillis(currentRealTime);
return calInstance;
}
Get a UTC timezone.
public static TimeZone getUTCTimeZone() {
return TimeZone.getTimeZone("GMT");
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents