IN PROGRESS

! new in Java 8 !

Import
java.time.*

LocalDate, LocalTime, LocalDateTime, ZonedDateTime

LocalDate represents a date in ISO format (yyyy-MM-dd).
LocalDate,…, ZonedDateTime;
– are static methods (have private constructors)
– are immutable, this means that adjusted date/time needs to be reassigned

Examples of usage:
LocalDate localDate = LocalDate.now();
LocalDate.of(2015, 02, 20);
LocalDate.parse("2015-02-20");
LocalDate tomorrow = LocalDate.now().plusDays(1);
LocalDate previousMonthSameDay = LocalDate.now().minus(1, ChronoUnit.MONTHS);
DayOfWeek sunday = LocalDate.parse("2016-06-12").getDayOfWeek();
int twelve = LocalDate.parse("2016-06-12").getDayOfMonth();
boolean leapYear = LocalDate.now().isLeapYear();

Java counts starting with 0 but months are an exception. For months in the new date and time methods, Java counts starting from 1.

Month month = Month.JANUARY;
boolean b1 = month == 1; // DOES NOT COMPILE
boolean b2 = month == Month.APRIL; // false

Instant, Period, Duration, and TemporalUnit
Localization

Date

ZonedId.of(“US/Eastern”)
ZonedDateTime.of(ix7, zone) x3 (no option to pass in the month enum!)
are immutable -> have to assign
Chaining: LocalDate.of(i, i, i).plusDays(1).minusWeeks(3)

Period
Period.ofMonths(1);
date.plus(period);
ofYears, ofMoths, ofWeeks, ofDays, of(i, i, i)
can’t chain – only last method is used

Printing period:
zeros are omitted
P1Y2M3D
Period.of(0, 20, 47) -> P20M47D
Period.ofWeeks(3) -> P21D – converted to days

Duration
ofDays, ofHours, ofMinutes, ofSeconds, ofMillis, ofNanos
Duration.of(3, ChronoUnit.DAYS)
ChronoUnit.HALF_DAYS
TemporalUnit is interface and ChronoUnit is one of imple.

ChronoUnit
ChronoUnit.HOURS.between(oneTime, twoTime) -> truncates

Instant = moment in time
Instant.now()
Duration.between(now, later)
zonedDateTime.toInstant()- can’t localDateTime
instant.plus(2, ChronoUnit.HOURS);

Methods
plusYears,…,plusWeeks,… – minusNanos

Exceptions/won’t compile
new LocalDate(); // won’t compile
LocalDate.of(2018, 6, 32); // DateTimeException
LocalDate.of(2014, 2, 29); Java gives us 28, cause 29 doesn’t exist
date.plusHours(1); // won’t compile
time.plus(period) // UnsupportedTemporalTypeException
ChronoUnit.HOURS.between(oneTime, twoDate) //DateTimeException
date.plus(Duration.ofDays(3)) // Unsupported unit: Seconds
instant.plus(2, ChronoUnit.WEEKS); // exception

Localization
Locale.getDefault()
fr or en_US (US, enUS, US_en, EN)
Locale.GERMAN
Locale.GERMANY
new Locale(“fr”), new Locale(“hi”, “IN”) – case insensitive, can be invalid or blank
new Locale.Builder().setLanguage(“en”).setRegion(“US”).build(); – any order, case insensitive , can be blank
Java will create an invalid locale, but won’t find the properties file

Locale.setDefault(locale);

Resource Bundle
if we don’t have country specific, language specific will be used
Property File
Zoo_en.properties
ResourdeBundle.getBundle(“Zoo”, locale);
rb.getString(“”);