## Sample Code The following examples work on Java 8+ ```java // let's create a date and print it out LocalDate birthday = LocalDate.of(1996, Month.NOVEMBER, 12); System.out.println(birthday); //1996-11-12 // Now let's print it out in more friendly format DateTimeFormatter birthdayFormat = DateTimeFormatter.ofPattern("dd MMMM yy"); System.out.println(birthday.format(birthdayFormat)); //12 November 96 //What day were you born on? System.out.println(birthday.getDayOfWeek()); //TUESDAY //Remind me two weeks before the birthday so I can send a card System.out.println("Buy card on " + birthday.minusWeeks(2).format(DateTimeFormatter.ofPattern("dd MMMM"))); //Buy card on 29 October //How old is this person? Finding the difference between dates //First find out today's date LocalDate today = LocalDate.now(); System.out.println(today); //2016-01-08 //Now work out the period between their birthday and today Period age = Period.between(birthday, today); System.out.println(age); //P19Y1M27D // Tidy that up System.out.println("You are " + age.getYears() + " years old"); //You are 19 years old //Let's have that in days long timeGapInDays = ChronoUnit.DAYS.between(birthday,today); System.out.println("Or, more precisely, " + timeGapInDays + " days old"); //Or, more precisely, 6996 days old //How do I convert a String to a LocalDate? // J.S. Bach was born on 31st March 1685 String bach = "31/03/1685"; //Tell Java what format the String is in DateTimeFormatter dateInput = DateTimeFormatter.ofPattern("dd/MM/yyyy"); //Now parse that String LocalDate bachBirthday = LocalDate.parse(bach, dateInput); System.out.println(bachBirthday); //1685-03-31 //Times are pretty much the same LocalTime workStarts = LocalTime.of(8, 50); LocalTime current = LocalTime.now(); System.out.println(workStarts); System.out.println(current); //08:50 //15:17:06.887 //I only want to see the hour and minute... DateTimeFormatter myTimeFormat = DateTimeFormatter.ofPattern("HH:mm"); System.out.println(current.format(myTimeFormat)); //15:17 //How long have I been at work? Duration timeAtWork = Duration.between(current, workStarts); System.out.println(timeAtWork); //PT-6H-27M-6.887S //Finally, what about DateTime? //Flash, I love you, but we only have 14 hours to save the Earth! LocalDateTime start = LocalDateTime.now(); LocalDateTime end = start.plusHours(14); System.out.println("The current DateTime is " + start); System.out.println("The Earth must be saved by " + end); //The current DateTime is 2016-01-08T15:17:06.887 //The Earth must be saved by 2016-01-09T05:17:06.887 //Let's format that DateTimeFormatter countdown = DateTimeFormatter.ofPattern("HH:mm, dd/MM/yyy"); System.out.println("Clock starts at " + start.format(countdown)); System.out.println("Boom! at " + end.format(countdown)); //Clock starts at 15:17, 08/01/2016 //Boom! at 05:17, 09/01/2016 ``` ## Exercises 1. Create a LocalDate for next Christmas 2. Output the day on which next Christmas occurs 3. Output the number of days until next Christmas 4. Write a program that outputs the number of days in the current month 5. Dolly Parton works from 9 to 5. Write a program to find how many minutes that is. 6. A TV program is transmitted at 17:10 hours. Write a program that outputs the time to wait until transmission in the format HH:mm 7. A firm promises delivery within 36 hours. Write a program that outputs the LocalDateTime in 36 hours in following format: 1526 hours Thursday 14 January, 2014