DATE AND TIME. Version Three. ============================ DATE AND TIME do NOT use "new" to create objects. Date and Time have static methods - they belong to the class, not the created object. We use "of" and "parse" and "now" yyyy-MM-dd is the default format for dates HH:mm:ss is the default format for time YYYY-MM-DDTHH:mm:ss is the default format for time/date. NOTE: String and String handling methods can also be used when dealing with date and time except when sorting and math is needed. =============================================================== OVERVIEW of creating date objects =============================================================== // date 1 - OVERVIEW - null, of, parse, DateTimeFormatter.ofPattern, now, between import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class DateAndTime1 { public static void main(String[]args) { LocalDate theDate1 = null; // works // LocalDate theDate2 = new LocalDate(); // does not work LocalDate theDate2 = LocalDate.of(2020, 2, 2); LocalDate theDate3 = LocalDate.parse("2020-02-02"); LocalDate theDate4 = LocalDate.parse("19 Feb 2020", DateTimeFormatter.ofPattern("dd MMM yyyy")); LocalDate theDate5 = LocalDate.now(); System.out.println(theDate1); System.out.println(theDate2); System.out.println(theDate3); System.out.println(theDate4); System.out.println(theDate5); } } OUTPUT ----jGRASP exec: java DateAndTime1 null 2020-02-02 2020-02-02 2020-02-19 2026-05-22 ----jGRASP: Operation complete. =============================================================== Static, non-static time and date methods =============================================================== We cannot call a method successfully unless we know whether the method belongs to the class (static) or to the created object (non-static) The following methods are static . . . now, of, parse Period.between, Duration.between DateTimeFormatter.ofPattern ofSecondOfDay ChronoUnit.DAYS.between The following methods are non static . . . all the getter methods e.g. getMonth, getHour etc isLearYear isEqual, isBefore, isAfter all the plus methods e.g. plusDays, plusYears, plusHours all the minus methods e.g. minusMonths, minusWeeks, minusMinutes format getSeconds // returns a long integer datatype Don't get confused with the two different "between" methods . . . Duration.between( ) // difference between times Period.between ( ) // difference between dates =============================================================== Date only - LocalDate - of =============================================================== Using the static method "of" to create a date object METHOD ANALYSIS TABLE --------------------- name: of class: LocalDate static parameters: 3 integers - yyyy-MM-dd // no leading zeros return type: void description: Creating a date object using LocalDate example: theDate1 = LocalDate.of(2020, 6, 9); ======================================================= // date 2 import java.time.LocalDate; public class DateAndTime2 { public static void main(String[]args) { LocalDate theDate1 = null; // three integer parameters - no leading zeros theDate1 = LocalDate.of(2020, 6, 9); // "of" is a static method of the LocalDate class System.out.println(theDate1); } } ======================================================= // date 3 import java.time.LocalDate; public class DateAndTime3 { public static void main(String[]args) { LocalDate theDate1 = null; // works // The following does NOT work as we are not creating any new objects // Date and time are static LocalDate theDate2 = new LocalDate(); } } ============================================================= The role of "toString" with ANY created object ============================================================= // date 4 import java.time.LocalDate; public class DateAndTime4 { public static void main(String[]args) { LocalDate theDate1 = null; theDate1 = LocalDate.of(2020, 6, 9); // created object // Following does not work as year, month and day properties are private // System.out.println(theDate1.year); // System.out.println(theDate1.month); // System.out.println(theDate1.day); // The toString works because this is a public method // toString is the way to get the private properties "out" // Being string these values are not a security risk System.out.println(theDate1.toString()); } } OUTPUT ----jGRASP exec: java DateAndTime4 2020-06-09 ----jGRASP: operation complete. ========================================================= Date - The getters methods. (Accessor methods) ========================================================= // date 5 // // getDayOfMonth // getDayOfWeek // getDayOfYear // getMonthValue - returns a number // getMonth - returns a String // getYear import java.time.LocalDate; public class DateAndTime5 { public static void main(String[]args) { LocalDate theDate1 = null; theDate1 = LocalDate.of(2020, 6, 9); System.out.println(theDate1); System.out.println(theDate1.getYear()); // 2020 System.out.println(theDate1.getMonth()); // JUNE System.out.println(theDate1.getDayOfMonth()); // 9 System.out.println(theDate1.getDayOfWeek()); // TUESDAY System.out.println(theDate1.getDayOfYear()); // 161 } } ============================================================= // date 6 import java.time.LocalDate; public class DateAndTime6 { public static void main(String[]args) { LocalDate theDate1 = null; theDate1 = LocalDate.of(1, 6, 9); // Goes all the way back to year one System.out.println(theDate1); System.out.println(theDate1.getYear()); // 1 System.out.println(theDate1.getMonth()); // JUNE System.out.println(theDate1.getDayOfMonth()); // 9 System.out.println(theDate1.getDayOfWeek()); // SATURDAY System.out.println(theDate1.getDayOfYear()); // 160 } } =============================================================== // date 7 import java.time.LocalDate; public class DateAndTime7 { public static void main(String[]args) { LocalDate theDate1 = null; theDate1 = LocalDate.of(-100000, 6, 9); // Goes all the way back to year one and beyond System.out.println(theDate1); System.out.println(theDate1.getYear()); // -100000 System.out.println(theDate1.getMonth()); // JUNE System.out.println(theDate1.getDayOfMonth()); // 9 System.out.println(theDate1.getDayOfWeek()); // SATURDAY System.out.println(theDate1.getDayOfYear()); // 160 } } ======================================================= Using the static method "parse" to create a date object ======================================================= Default format: yyyy-MM-dd. Note capital "M" to avoid confusion with minutes "m" Using the static method "parse" to create a date object METHOD ANALYSIS TABLE --------------------- name: parse class: LocalDate static parameters: 1) one string, 2) one string with DateTimeFormatter.ofPattern return type: void description: Creating a date object using LocalDate example1: theDate1 = LocalDate.parse("2020-08-12"); // follow exact template example2: theDate4 = LocalDate.parse("19 Feb 2020", DateTimeFormatter.ofPattern("dd MMM yyyy")); ========================================================= // date 8 import java.time.LocalDate; public class DateAndTime8 { public static void main(String[]args) { LocalDate theDate1 = null; // one string yyyy-MM-dd theDate1 = LocalDate.parse("2020-06-04"); // parse is a static method of the LocalDate class System.out.println(theDate1); } } ============================================================================== Starting with a different date format - use "DateTimeFormatter.ofPattern" ============================================================================== METHOD ANALYSIS TABLE --------------------- name: ofPattern class: DateTimeFormatter non static parameters: one return type: String description: Changing the appearance of an existing date object example: theDate1 = LocalDate.parse("2021-06-01", DateTimeFormatter.ofPattern("yyyy-MM-dd")); // date 9 // two arguements - // 1) the date // 2) the explanation of the date format using ofPattern method // ofPattern is a static method of the DateTimeFormatter class // MMM means the month Jun (not JUN), Jan (not JAN) // MM means the number of the month // Feb 31 will yield an output of Feb 28 // Feb 32 and the program will throw DateTimeException theDate1 = LocalDate.parse("2021-06-01", DateTimeFormatter.ofPattern("yyyy-MM-dd")); System.out.println(theDate1); theDate1 = LocalDate.parse("01/12/2019", DateTimeFormatter.ofPattern("dd/MM/yyyy")); System.out.println(theDate1); theDate1 = LocalDate.parse("04-Jun-2018", DateTimeFormatter.ofPattern("dd-MMM-yyyy")); System.out.println(theDate1); theDate1 = LocalDate.parse("Jun 27, 2019", DateTimeFormatter.ofPattern("MMM dd, yyyy")); System.out.println(theDate1); OUTPUT ----jGRASP exec: java DateAndTime 2021-06-01 2019-12-01 2018-06-04 2019-06-27 ----jGRASP: operation complete. ================================================================================ Changing the date format later - use "DateTimeFormatter.ofPattern" Non-static so we must first create a DateTimeFormatter object ================================================================================ METHOD ANALYSIS TABLE --------------------- name: format class: DateTimeFormatter non static parameters: one return type: String description: Changing the appearance of an existing date object example: String newDateFormat = dmy.format(theDate2); // date 10 import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class DateAndTime10 { public static void main(String[]args) { LocalDate theDate2 = LocalDate.of(2020, 2, 2); System.out.println(theDate2); // To change the date format we use the Non static "format" method // Therefore we need a DateTimeFormatter object // Do not use "new" to create this object DateTimeFormatter dmy = DateTimeFormatter.ofPattern("dd-MM-yyyy"); String newDateFormat = dmy.format(theDate2); System.out.println(newDateFormat); } } ====================================================================================== Current date, current time, current date and time together - now( ) ====================================================================================== // date time 11 import java.time.LocalDate; import java.time.LocalTime; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class DateAndTime11 { public static void main(String[]args) { LocalDate date1 = LocalDate.now(); System.out.println(date1); LocalTime time1 = LocalTime.now(); System.out.println(time1); LocalDateTime dateTime1 = LocalDateTime.now(); System.out.println(dateTime1); // parsing the time to a String for further processing later String theNumber = "" + time1; System.out.println(theNumber); } } OUTPUT ---- jGRASP exec: java DateAndTime11 2022-11-25 11:03:03.576 2022-11-25T11:03:03.576 11:03:03.576 ----jGRASP: operation complete. ===================================================================== Time only - LocalTime - of ===================================================================== HH:mm:ss is the default format for time // time 12 import java.time.LocalTime; public class Time12 { public static void main(String[]args) { LocalTime theTime1, theTime2 = null; // four integer parameters // hour, minute, second, nano theTime1 = LocalTime.of(3, 4, 5, 6); // "of" is a static method of the LocalTime class theTime2 = LocalTime.of(3, 4); System.out.println(theTime1); System.out.println(theTime2); } } OUTPUT ----jGRASP exec: java Time12 03:04:05.000000006 03:04 ----jGRASP: operation complete. ========================================================================== METHOD ANALYSIS TABLE --------------------- name: of class: LocalTime static parameters: 4 integers - hour, min, sec, nano return type: void description: Creating a time object using LocalTime example: theTime1 = LocalTime.of(3, 4, 5, 6); ===================================================================== Time only - LocalTime - parse ===================================================================== Using the static method "parse" to create a time object METHOD ANALYSIS TABLE --------------------- name: parse class: LocalTime static parameters: 1) one string, 2) one string with DateTimeFormatter.ofPattern return type: void description: Creating a date object using LocalTime example1: theTime1 = LocalTime.parse("15:34:58"); // follow exact template example2: theTime4 = LocalTime.parse("55 34 12", DateTimeFormatter.ofPattern("ss mm HH")); // time 13 - Null, parse, using DateTimeFormatter import java.time.LocalTime; import java.time.format.DateTimeFormatter; public class Time13 { public static void main(String[]args) { LocalTime theTime1 = null; // Default format HH:mm:ss LocalTime theTime5 = LocalTime.parse("20:09:01"); // Not the default format - second arguement is needed //LocalTime theTime6 = LocalTime.parse("23 59 35"); // does not run LocalTime theTime7 = LocalTime.parse("23 59 35", DateTimeFormatter.ofPattern("HH mm ss")); LocalTime theTime8 = LocalTime.parse("01 02 03", DateTimeFormatter.ofPattern("ss mm HH")); System.out.println(theTime5); System.out.println(theTime7); System.out.println(theTime8); } } OUTPUT ----jGRASP exec: java Time13 20:09:01 23:59:35 03:02:01 ----jGRASP: Operation complete. ====================================================================== Date and time together - LocalDateTime - of - parse ====================================================================== YYYY-MM-DDTHH:mm:ss is the default format for time/date. The "T" is a useful delimiter separating the date from the time. // Date and time 14 import java.time.LocalDateTime; public class DateAndTime14 { public static void main(String[]args) { LocalDateTime theDate1 = null; // 7 arguements - year, month, day, hour, minutes, seconds, naonseconds LocalDateTime theDate2 = LocalDateTime.of(2020, 2, 2, 23, 41, 16, 987); LocalDateTime theDate3 = LocalDateTime.parse("2020-02-02T23:41:16"); // cannot parse nano System.out.println(theDate1); System.out.println(theDate2); System.out.println(theDate3); System.out.println(theDate2.toString()); } } OUTPUT ----jGRASP exec: java DateAndTime14 null 2020-02-02T23:41:16.000000987 2020-02-02T23:41:16 2020-02-02T23:41:16.000000987 ----jGRASP: operation complete. ======================================================================== Time - Getter methods - (accessor methods) ======================================================================== // Time 15 - getter methods. All return integers // getHour // getMinutes // getSecond // getNano import java.time.LocalTime; import java.time.format.DateTimeFormatter; public class Time15 { public static void main(String[]args) { // Default format HH:mm:ss LocalTime theTime2 = LocalTime.of(23, 59, 35, 234); LocalTime theTime3 = LocalTime.parse("20:09:01"); System.out.println(theTime2.getHour()); System.out.println(theTime2.getMinute()); System.out.println(theTime3.getSecond()); } } ----jGRASP exec: java Time15 23 59 1 ----jGRASP: Operation complete. ======================================================================== Getter methods and maths ======================================================================== import java.time.LocalDate; import java.lang.Math; public class DateAndTime2 { public static void main(String[]args) { LocalDate theDate1 = null, theDate2 = null; theDate1 = LocalDate.of(2018, 6, 9); theDate2 = LocalDate.of(2020, 6, 9); System.out.println(theDate1); // maths is possible when the values are stored as numbers int year1 = theDate1.getYear(); int year2 = theDate2.getYear(); int month1 = theDate1.getMonthValue(); // the value, not the name int month2 = theDate1.getMonthValue(); // Always want the difference to be a positive number System.out.println("The year difference is " + Math.abs(year1 - year2)); System.out.println("The month difference is " + Math.abs(month1 - month2)); } } OUTPUT 2018-06-09 The year difference is 2 The month difference is 0 ----jGRASP: operation complete. ======================================================================== Sorting - isEqual, isBefore, isAfter. Returns boolean Bubble sort algorithm. ======================================================================== METHOD ANALYSIS TABLE --------------------- name: isBefore class: LocalTime static parameters: one (date object) return type: boolean description: Determines if one date is before another example: date2.isBefore(date1) ======================================================================== // date time 16 // "isBefore" is used in the bubble sort algorithm to sort dates import java.time.LocalDate; public class DateAndTime16 { public static void main(String[]args) { LocalDate[] dateArray = new LocalDate[5]; LocalDate temp = LocalDate.of(1, 1, 1); dateArray[0] = LocalDate.of(2020, 8, 12); dateArray[1] = LocalDate.of(2019, 10, 29); dateArray[2] = LocalDate.of(2019, 1, 8); dateArray[3] = LocalDate.of(2020, 12, 6); dateArray[4] = LocalDate.of(2020, 7, 12); System.out.println("Original order"); for(int i = 0; i < 5; i++) System.out.println(dateArray[i]); // bubble sort algorithm for(int i = 0; i < 5;i++) { for(int k = i + 1; k < 5; k++){ LocalDate date1 = dateArray[i]; LocalDate date2 = dateArray[k]; // isBefore returns boolean if(date2.isBefore(date1)) { temp = dateArray[i]; dateArray[i] = dateArray[k]; dateArray[k] = temp; } // end if } // inner loop } // outer loop System.out.println("Sorted Array oldest to newest"); for(int i = 0; i < 5; i++) System.out.println(dateArray[i]); } } OUTPUT ----jGRASP exec: java DateAndTime16 Original order 2020-08-12 2019-10-29 2019-01-08 2020-12-06 2020-07-12 Sorted Array oldest to newest 2019-01-08 2019-10-29 2020-07-12 2020-08-12 2020-12-06 ----jGRASP: operation complete. ================================================================= Adding and subtracting days, weeks, months and years ================================================================= // Date calculations 17 // plusYears // plusMonths // plusDays // plusWeeks // // minusYears // minusMonths // minusDays // minusWeeks import java.time.LocalDate; import java.lang.Math; public class DateAndTime17 { public static void main(String[]args) { LocalDate startFast = LocalDate.of(2021, 6, 9); LocalDate endFast = startFast.plusDays(40); System.out.println("Start Fast: " + startFast); System.out.println("End Fast: " + endFast); LocalDate startHomeLoan = LocalDate.of(2021, 6, 9); LocalDate endHomeLoan = startFast.plusYears(20); System.out.println("Start Home Loan: " + startHomeLoan); System.out.println("End Home Loan: " + endHomeLoan); LocalDate endCourse = LocalDate.of(2021, 01, 9); LocalDate startCourse = startFast.minusWeeks(50); System.out.println("Started Course: " + startCourse); System.out.println("Ended Course: " + endCourse); } } OUTPUT ----jGRASP exec: java DateAndTime6 Start Fast: 2021-06-09 End Fast: 2021-07-19 Start Home Loan: 2021-06-09 End Home Loan: 2041-06-09 Start Course: 2020-06-24 End Course: 2021-01-09 ----jGRASP: operation complete. ============================================================================= Combining methods above to compare and do math ============================================================================= // date calculations 18 LocalDate date3 = LocalDate.of(2025,1,1); LocalDate date4 = LocalDate.of(2023,03,7); if(date3.isAfter(date4.plusDays(2))) System.out.println("Yes"); else System.out.println("No"); if(date3.isAfter(date4.plusMonths(3))) System.out.println("Yes"); else System.out.println("No"); if(date3.isAfter(date4.plusYears(4))) System.out.println("Yes"); else System.out.println("No"); OUTPUT ----jGRASP exec: java NumberOfDays Yes Yes No ----jGRASP: Operation complete. ============================================================================= Adding a static final variable to the mix. Building an progressive report message. Uses the String method "isBlank". Note that "isBlank" is not equal to "isEmpty" ============================================================================= Date calculations 19 - VERSION ONE ================================= public class MoreDateTimeMaths { public static final int FACTOR_26 = 2; public static void main(String[]args) { String message = ""; LocalDate date3 = LocalDate.of(2025,1,1); LocalDate date4 = LocalDate.of(2023,03,7); if(date3.isBefore(date4.plusDays(FACTOR_26))) message = message + "Test One. Fail\n"; if(date3.isBefore(date4.plusMonths(FACTOR_26 + 4))) message = message + "Test Two. Fail\n"; if(date3.minusYears(7).isAfter(date4.plusMonths(FACTOR_26 + 4))) message = message + "Test Three. Fail"; if(message.isBlank()) System.out.println("All tests passed"); else System.out.println(message); } } OUTPUT ----jGRASP exec: java MoreDateTimeMaths All tests passed ----jGRASP: Operation complete. Date calculations 20 - VERSION TWO ================================== public class MoreDateTimeMaths { public static final int FACTOR_26 = 2; public static void main(String[]args) { String message = ""; LocalDate date3 = LocalDate.of(2025,1,1); LocalDate date4 = LocalDate.of(2023,03,7); if(date3.isAfter(date4.plusDays(FACTOR_26))) message = message + "Test One. Fail\n"; if(date3.isAfter(date4.plusMonths(FACTOR_26 + 4))) message = message + "Test Two. Fail\n"; if(date3.minusYears(7).isBefore(date4.plusMonths(FACTOR_26 + 4))) message = message + "Test Three. Fail"; if(message.isBlank()) System.out.println("All tests passed"); else System.out.println(message); } } OUTPUT ----jGRASP exec: java MoreDateTimeMaths Test One. Fail Test Two. Fail Test Three. Fail ----jGRASP: Operation complete. ============================================================================= More maths - calculating the difference between dates and times ============================================================================== Period class - for the difference in years, months and days Duration class - for the time difference in seconds and nanoseconds ============================================================================= METHOD ANALYSIS TABLE --------------------- name: between class: Period static parameters: two date objects return type: Period object description: Determines the period between two dates in years, months and days example: Period theDifference = Period.between(dob, today); ============================================================================== // Date difference calculations 21 import java.time.LocalDate; import java.time.Period; public class TimeDifference21 { public static void main(String[]args) { LocalDate dob = LocalDate.of(2002,3,11); LocalDate today = LocalDate.now(); Period theDifference = Period.between(dob, today); System.out.println(dob + " " + today + " " + theDifference); } } OUTPUT ----jGRASP exec: java TimeDifference21 2002-03-11 2026-05-23 P24Y2M12D // Period is 24 years, 2 months and 12 days. ----jGRASP: Operation complete. ============================================================================= Period class - getter methods for years, months and days ============================================================================== // Date difference calculations 22 static - getYears, getMonths, getDays For a more user friendly output. import java.time.LocalDate; import java.time.Period; public class TimeDifference22 { public static void main(String[]args) { LocalDate dob = LocalDate.of(2002,3,11); LocalDate today = LocalDate.now(); Period theDifference = Period.between(dob, today); int years = theDifference.getYears(); int months = theDifference.getMonths(); int days = theDifference.getDays(); System.out.println("The difference is: " + years + " years " + months + " months " + days + " days"); } } OUTPUT ----jGRASP exec: java TimeDifference22 The difference is: 24 years 2 months 12 days ----jGRASP: Operation complete. ============================================================================= The methods above are not that useful because many times we need the number of days, in days (not years, months and days) We cannot code with years, months and days - we need the number of days exactly e.g. 128 days, 493 days etc so that we can code conditional statements and loops with a known numerical value NOTE: ChronoUnit is not in the SAGS. When you use it, place a comment into your code. ============================================================================== ChronoUnit class - for the day difference between two given dates. NOTE: Following code was compiled and run using JDK17 =============================================================================== METHOD ANALYSIS TABLE --------------------- name: DAYS.between class: ChronoUnit static parameters: two LocalTime objects return type: long integer description: Determines the exact day difference two given dates example: long daysBetween = ChronoUnit.DAYS.between(date1, date2); ============================================================================== // Date calculations 23 // The exact number of days using the ChronoUnit library import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class NumberOfDays { public static void main(String[]args) { LocalDate date1 = LocalDate.of(2020,01,01); LocalDate date2 = LocalDate.of(2026,06,25); long daysBetween = ChronoUnit.DAYS.between(date1, date2); System.out.println("number of days is " + daysBetween); } } OUTPUT ----jGRASP exec: java NumberOfDays number of days is 2367 ----jGRASP: Operation complete. =============================================================================== Duration class - for the time difference between two given times. NOTE: Following code was compiled and run using JDK17 =============================================================================== METHOD ANALYSIS TABLE --------------------- name: between class: Duration static parameters: two LocalTime objects return type: Duration object description: Determines the time period between two given time objects example: Duration diff = Duration.between(t1,t2); ============================================================================== // Time difference calculations 24 import java.time.LocalTime; import java.time.Duration; public class TimeDifference24 { public static void main(String[]args) { LocalTime t1 = LocalTime.of(2,37,16); LocalTime t2 = LocalTime.of(18,4,6); Duration diff = Duration.between(t1,t2); System.out.println(diff); } } OUTPUT ----jGRASP exec: java TimeDifference24 PT16H26M51S ----jGRASP: Operation complete. ============================================================================= Duration objects does not have a getter for hours or minutes - only seconds. This is a problem. Below are two approaches to get around this challenge. =============================================================================== // Time difference calculations 25 import java.time.LocalTime; import java.time.Duration; public class TimeDifference25 { public static void main(String[]args) { LocalTime t1 = LocalTime.of(2,37,16); LocalTime t2 = LocalTime.of(19,4,7); Duration diff = Duration.between(t1,t2); System.out.println("Duration object: " + diff); String durationBetween = "" + diff; System.out.println("String: " + durationBetween); long seconds = diff.getSeconds(); System.out.println("Number of seconds: " + seconds); } } OUTPUT ----jGRASP exec: java TimeDifference25 Duration object: PT16H26M51S String: PT16H26M51S Number of seconds: 59211 ----jGRASP: Operation complete. ========================================================================== // Time difference calculations 26 A more user friendly version. METHOD ONE using LocalDate getters import java.time.LocalTime; import java.time.Duration; public class TimeDifference26 { public static void main(String[]args) { LocalTime t1 = LocalTime.of(2,37,16); LocalTime t2 = LocalTime.of(19,4,7); Duration diff = Duration.between(t1,t2); System.out.println("Duration object: " + diff); String durationBetween = "" + diff; System.out.println("String: " + durationBetween); // getSeconds returns a long integer long seconds = diff.getSeconds(); System.out.println("Number of seconds: " + seconds); // Converts a number of seconds into hours, minutes and seconds // and then creates a LocalTime object LocalTime timeDiff = LocalTime.ofSecondOfDay(seconds); int hours = timeDiff.getHour(); int mins = timeDiff.getMinute(); int secs = timeDiff.getSecond(); System.out.println(hours + " hours " + mins + " mins " + secs + " seconds"); } } OUTPUT ----jGRASP exec: java TimeDifference26 Duration object: PT16H26M51S String: PT16H26M51S Number of seconds: 59211 16 hours 26 mins 51 seconds ----jGRASP: Operation complete. ========================================================================== // Time difference calculations 27 A more user friendly version. METHOD TWO using String methods import java.time.LocalTime; import java.time.Duration; public class TimeDifference27 { public static void main(String[]args) { LocalTime t1 = LocalTime.of(2,37,16); LocalTime t2 = LocalTime.of(19,4,7); Duration diff = Duration.between(t1,t2); System.out.println("Duration object: " + diff); String durationBetween = "" + diff; System.out.println("String: " + durationBetween); long seconds = diff.getSeconds(); System.out.println("Number of seconds: " + seconds); String hours = durationBetween.substring(2,4); String minutes = durationBetween.substring(5,7); String secs = durationBetween.substring(8,10); System.out.println(hours + " hours " + minutes + " mins " + secs + " seconds"); } } OUTPUT ----jGRASP exec: java TimeDifference27 Duration object: PT16H26M51S String: PT16H26M51S Number of seconds: 59211 16 hours 26 mins 51 seconds ----jGRASP: Operation complete.