Skip to content

Instantly share code, notes, and snippets.

@vipulshah2010
Last active September 9, 2020 09:13
Show Gist options
  • Save vipulshah2010/3c6b64bc73749d904ac62134c517f64e to your computer and use it in GitHub Desktop.
Save vipulshah2010/3c6b64bc73749d904ac62134c517f64e to your computer and use it in GitHub Desktop.
Time difference
boolean isValid(Date date1, Date date2) {
Timestamp timestamp1 = new Timestamp(date1.getTime());
Timestamp timestamp2 = new Timestamp(date2.getTime());
long milliseconds = timestamp2.getTime() - timestamp1.getTime();
int seconds = (int) milliseconds / 1000;
int hours = seconds / 3600;
int minutes = (seconds % 3600) / 60;
seconds = (seconds % 3600) % 60;
System.out.println("timestamp1: " + timestamp1);
System.out.println("timestamp2: " + timestamp2);
System.out.println("Difference: ");
System.out.println(" Hours: " + hours);
System.out.println(" Minutes: " + minutes);
System.out.println(" Seconds: " + seconds);
return hours < 24;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment