DATE AND TIME ** DATE AND TIME does NOT use NEW to create objects We use "of" and "parse" yyyy-MM-dd is the default format for dates HH mm ss is the default formate for time YYYY-MM-DDTHH:mm:ss is the default format for time/date =============================================================== Date only - LocalDate =============================================================== 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 first 1 import java.time.LocalDate; public class DateAndTime1 { 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 first 1B import java.time.LocalDate; public class DateAndTime1B { public static void main(String[]args) { LocalDate theDate1 = null; // works LocalDate theDate2 = new LocalDate(); // does not work } } =============================================================== // date first 1C - overview. Null, using of, using parse, using DateTimeFormatter import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class DateAndTime1C { 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")); System.out.println(theDate1); System.out.println(theDate2); System.out.println(theDate3); System.out.println(theDate4); } } OUTPUT ----jGRASP exec: java DateAndTime2 null 2020-02-02 2020-02-02 2020-02-19 ----jGRASP: operation complete. ============================================================= The role of "toString" with any created object ============================================================= // date 2 import java.time.LocalDate; public class DateAndTime2 { public static void main(String[]args) { LocalDate theDate1 = null; theDate1 = LocalDate.of(2020, 6, 9); // created object // 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 DateAndTime3 2020-06-09 ----jGRASP: operation complete. ========================================================= The getters methods. (Accessor methods) ========================================================= // date 3 // // getDayOfMonth // getDayOfWeek // getDayOfYear // getMonthValue // getMonth // getYear // // getHour // getMinutes // getSecond // getNano import java.time.LocalDate; public class DateAndTime3 { 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 4 import java.time.LocalDate; public class DateAndTime4 { 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 5 import java.time.LocalDate; public class DateAndTime5 { 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 "of" to create a date object ============================================================ METHOD ANALYSIS TABLE --------------------- name: of class: LocalDate static parameters: 3 integers - yyyy-MM-dd return type: void description: Creating a date object using LocalDate example: theDate1 = LocalDate.of(2020, 6, 9); ======================================================= Using the static method "parse" to create a date object ======================================================= yyyy-MM-dd 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 example: theDate1 = LocalDate.parse("2020-08-12"); // follow exact template ========================================================= // date 6 import java.time.LocalDate; public class DateAndTime6 { 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" ============================================================================== // date 7 import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class DateAndTime7 { public static void main(String[]args) { LocalDate theDate1 = null; // 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 yeild an output of Feb 28 // Feb 32 and the program will crash with a 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. ================================================================================ Changeing 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 8 import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class DateAndTime8 { 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 ====================================================================================== // date 9 import java.time.LocalDate; import java.time.LocalTime; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class DateAndTime9 { 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 DateAndTime8 2022-11-25 11:03:03.576 2022-11-25T11:03:03.576 11:03:03.576 ----jGRASP: operation complete. ===================================================================== Time only - LocalDate ===================================================================== HH mm ss is the default format for time // time 1 import java.time.LocalTime; public class Time1 { 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 Time1 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 2 - overview. Null, using of, using parse, using DateTimeFormatter import java.time.LocalTime; import java.time.format.DateTimeFormatter; public class Time2 { public static void main(String[]args) { LocalTime theTime1 = null; LocalTime theTime2 = LocalTime.of(23, 59, 35, 234); // LocalTime theTime3 = LocalTime.parse("23 59 35 234"); second arguement is needed LocalTime theTime4 = LocalTime.parse("23 59 35", DateTimeFormatter.ofPattern("HH mm ss")); System.out.println(theTime1); System.out.println(theTime2); System.out.println(theTime4); } } OUTPUT ----jGRASP exec: java Time3 null 23:59:35.000000234 23:59:35 ----jGRASP: operation complete. ====================================================================== Date and time together - LocalDateTime ====================================================================== YYYY-MM-DDTHH:mm:ss is the default format for time/date // Date and time together1 import java.time.LocalDateTime; public class DateAndTime1 { 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 DateAndTime4 null 2020-02-02T23:41:16.000000987 2020-02-02T23:41:16 2020-02-02T23:41:16.000000987 ----jGRASP: operation complete. ======================================================================== Doing maths - More on getter methods ======================================================================== // Date and time getter methods 2 // // getDayOfMonth // getDayOfWeek // getDayOfYear // getMonthValue // getMonth // getYear // // getHour // getMinutes // getSecond // getNano 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 ----jGRASP exec: java DateAndTime5 2018-06-09 The year difference is 2 The month difference is 0 ----jGRASP: operation complete. ======================================================================== Doing maths - is equal, is before, is after ======================================================================== 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 5 // date calculations - is before, is equal, is after // "isBefore" is used in the bubble sort algorithm to sort dates import java.time.LocalDate; public class DateAndTime5 { 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 DateAndTime5 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 and time calculations 6 // plusYears // plusMonths // plusDays // plusWeeks // // minusYears // minusMonths // minusDays // minusWeeks import java.time.LocalDate; import java.lang.Math; public class DateAndTime6 { 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. =============================================================================