Compare Two Timestamps In Java
Chapter:
Date and Time
Last Updated:
26-04-2016 19:24:25 UTC
Program:
/* ............... START ............... */
import java.sql.Timestamp;
import java.util.Date;
public class JavaCompareTwoTimeStamps {
public static void main(String[] args) {
Timestamp timestamp1 = new Timestamp(new Date().getTime());
System.out.println(timestamp1);
Timestamp timestamp2 = new Timestamp(new Date().getTime());
System.out.println(timestamp2);
if (timestamp1.after(timestamp2)) {
System.out.println("timestamp1 is after timestamp2");
}
if (timestamp1.before(timestamp2)) {
System.out.println("timestamp1 is before timestamp2");
}
timestamp1 = new Timestamp(new Date().getTime());
timestamp2 = new Timestamp(timestamp1.getTime());
if (timestamp1.equals(timestamp2)) {
System.out.println("timestamp1 equals timestamp2");
}
}
}
/* ............... END ............... */
Output
2016-04-26 21:42:04.663
2016-04-26 21:42:04.674
timestamp1 is before timestamp2
timestamp1 equals timestamp2
Tags
Compare Two Timestamps, Java