Dates and times in are far more complicated than you might think.
To illustrate, ask yourself what's the time now? The answer will be different depending on where you are in the world and whether or not you are using daylight saving time.
There are similar problems using dates, for example, what will the date be in two months time? Do I have to take into account the fact that it's a leap year as I write this?
Java 8 included a complete redesign of Java's date and time packages. These notes will get you started, find out more by following the [date-time trail](https://docs.oracle.com/javase/tutorial/datetime/overview/index.html)
Let's create a date and print it out:
```java
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
```java
DateTimeFormatter birthdayFormat = DateTimeFormatter.ofPattern("dd MMMM yy");
System.out.println(birthday.format(birthdayFormat));
=> 12 November 96
```
What day were you born on?
```java
System.out.println(birthday.getDayOfWeek());
=> TUESDAY
```
Remind me two weeks before the birthday so I can send a card
``` java
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
```java
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?
```java
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
```java
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?
```java
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
```
Looking ahead...
```java
LocalDateTime now = LocalDateTime.now().plusMonths(6);
System.out.println(now.with(TemporalAdjusters.firstDayOfNextMonth()));
System.out.println(now.getDayOfWeek());
System.out.println(now.getHour());
System.out.println(now.getMonth());
System.out.println(now.getMonthValue());
=> 2016-08-01T15:17:06.887
=> FRIDAY
=> 15
=> JULY
=> 7
```
# 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