Detect if system time has been moved backwards in java, or time proof timers
By : James Gathings
Date : March 29 2020, 07:55 AM
it fixes the issue Pretty much all timers set a future time and then regularly compare current time to the given time. That's why timers "stall" when real time goes backward. Unfortunately, ALL of the timers in the JVM are related to the time of day. For example, java.util.Timer does a Object.wait(milliSeconds) for an event to fire. That boils down to a thread call that also does a wait, for t milliseconds. And it's always relative to the "time of day".
|
Compare the System date and time separately from database date and time to start another activity
By : Jooho Son
Date : November 22 2020, 11:05 AM
With these it helps You use String.compareTo() to compare the "time" String, which won't work correctly. Try changing the code to this code :
private void startTraining() throws ParseException {
// current date & time
Calendar now = Calendar.getInstance();
// parse date & time from database
String trainingStartDate = SharedMemory.getInstance()
.getCurrentTraining().getDate();
String trainingStartTime = SharedMemory.getInstance()
.getCurrentTraining().getStartTime();
String strDateTime = trainingStartDate + " " + trainingStartTime;
Calendar training = Calendar.getInstance();
training.setTime(new SimpleDateFormat("MM-dd-yyyy kk:mm")
.parse(strDateTime));
// find difference in milliseconds
long difference = training.getTimeInMillis() - now.getTimeInMillis();
if (difference < 15 * 60 * 1000) { //less than 15 minutes
Log.i("Debug", "CHECKPOINT");
Intent intent = new Intent(getApplicationContext(),
TraineeListActivity.class);
MainActivity.this.startActivity(intent);
finish();
} else {
}
}
|
Android: How to detect if user changes system time without running a constant service to check time
By : amogh
Date : March 29 2020, 07:55 AM
I wish this help you You can save the current time in shared preference and compare it every time you sample it. If you get an older sample the user changed the time backward.
|
How to do time comparison between client's system time and date time from internet server (actual local time)
By : Laurentiu Stoica
Date : March 29 2020, 07:55 AM
help you fix your problem The main question is IMHO what accuracy you need. You can just query a NTC and report if there is a discrepancy. If the server is synchronized with such a time server, there shouldn't be a problem. code :
bool compareTime() {
var serverTime = await getTimeFromServer(); // not yet existing method to fetch the date and time from the server
var clientTime = new DateTime.now().toUtc();
var diff = serverTime.difference(clientTime).abs();
if(diff > const Duration(seconds: 5)) {
print('Time difference too big');
} else {
print('Time is fine');
}
}
|
iOS how to detect user has changed system time, and if not compare device time with server time
By : cristian apple
Date : March 29 2020, 07:55 AM
hop of those help? As per its documentation, NSSystemClockDidChangeNotification is:
|