Please enable JavaScript.
Coggle requires JavaScript to display documents.
Working with Dates and Times (static (LocalDate.now(); (Creates date of…
Working with Dates and Times
Java 8 revamped API Date and Time
java.time.*
older api date and time is not on exam
static
LocalDate.now();
Creates date of today
LocalTime.now();
Creates actual hour with HH:mm:ss:nano
LocalDateTime.now();
Creates both actual
LocalDate.of(2015, Month.JANUARY, 20);
LocalDate.of(2015,1,20);
Do the same thing
LocalTime.of(6, 15);
LocalTime.of(6, 15, 30);
LocalTime.of(6, 15, 30, 200);
HH:mm
HH:mm:ss
HH:mm:ss:nanossegundos
LocalDateTime.of(date1, time1);
Tip
Tricks
LocalDate date = new LocalDate(): //Does not compile
LocalDate has private constructor
LocalDate.of(2015, Month.JANUARY, 32)
throw DateTimeException because of 32
LocalDate date = LocalDate.of(2020, Month.JANUARY, 20);
date.plusDays(10);
Sysout(date);
out is original date
date are immutable
Second line ignore result
LocalDate date = LocalDate.of(2020, Month.JANUARY, 20);
date = date.plusMinutes(1); //Does not compile
date doesn't have method plusMinutes();
only LocalTime and LocalDateTime has
pay attention in methods, can be a trick
Period period = Period.ofYears(1).ofWeeks(1);
Cannot use chain, the out will be
period.getYears(); //0
period.getDays(); //7
Correct it is:
Period period = Period.of(1, 0, 7);
period.getYears();//1
period.getDays();//7
Period period = Period.ofMonths(1);
LocalTime time = LocalTime.now();
time.plus(period);//UnsupportedTemporalTypeException
Format SHORT and MEDIUM will be on exam
Manipulating Dates and Times
static methods
Review table 3.4 page 144
Formatting Date and Time
DateTimeFormatter formats date and/or time
package java.time.format.
Review table 3.5 149
Parsing Date and Time
DateTimeFormatter.ofPattern("MM dd yyyy");