// Date and time // Are dates equal? True or false // Adding and subtracting days and hours to a date import java.time.*; public class WorkingWithDatesAndTime { public static void main (String[]args) { // Declare outside the try ...catch int year = 2012; int month = 4; int day = 17; int hours = 8; int mins = 43; int secs = 50; LocalDateTime ldt1 = null, ldt2 = null, ldt3 = null; // ldt1 is the same as ldt3 try { ldt1 = LocalDateTime.of(year, month, day, hours, mins, secs); ldt2 = LocalDateTime.of(2019, 6, 3, 7, 12, 23); ldt3 = LocalDateTime.of(2012, 4, 17, 8, 43,50); } catch (DateTimeException ex) { System.out.println("Error with date and time"); } // end catch System.out.println("The three dates are"); System.out.println("===================="); System.out.print(ldt1 + "\t"); System.out.print(ldt2 + "\t"); System.out.println(ldt3); System.out.println(); // Methods: isEqual, isBefore, isAfter System.out.println("Are any of the dates equal?"); System.out.println("===================="); System.out.print(ldt1.isEqual(ldt2) + "\t"); System.out.print(ldt1.isEqual(ldt3) + "\t"); System.out.println(ldt2.isEqual(ldt3)); System.out.println(); // Adding and subtracting days and or years System.out.println("Adding 50 days, subtracting 50 days"); System.out.println("Adding years and adding minutes"); System.out.println("==================================="); System.out.println("Plus days: " + ldt1.plusDays(50)); System.out.println("Subtract days: " + ldt1.plusDays(-50)); System.out.println("Plus years: " + ldt1.plusYears(50)); System.out.println("Plus minutes: " + ldt1.plusMinutes(50)); } // end main } ========================================================================== OUTPUT ----jGRASP exec: java WorkingWithDatesAndTime The three dates are ==================== 2012-04-17T08:43:50 2019-06-03T07:12:23 2012-04-17T08:43:50 Are any of the dates equal? ========================== false true false Adding 50 days, subtracting 50 days Adding years and adding minutes =================================== Plus days: 2012-06-06T08:43:50 Subtract days: 2012-02-27T08:43:50 Plus years: 2062-04-17T08:43:50 Plus minutes: 2012-04-17T09:33:50 ----jGRASP: operation complete.