import java.util.Date; public class Example { public static void main(String[] args) { Date now = new Date(); long timestamp = now.getTime(); System.out.println("Unix timestamp of current time: " + timestamp); } }
import java.util.Date; public class Example { public static void main(String[] args) { Date date1 = new Date(); Date date2 = new Date(2022, 0, 1); if (date1.getTime() < date2.getTime()) { System.out.println("date1 is before date2"); } else if (date1.getTime() > date2.getTime()) { System.out.println("date1 is after date2"); } else { System.out.println("date1 and date2 are equal"); } } }This code creates two Date objects, one representing the current time (date1) and another representing January 1, 2022 (date2). It then uses the getTime() method to get the Unix timestamp of each date, and compares them using standard if/else statements to determine which date is before or after the other, or if they are equal. The java.util.Date class is part of the Java Standard Library, which is included in the java.util package.