Did you know:
- Month.JANUARY is the same as Month.of(1)
- Month is an enum
- Month.JANUARY == 1 does not compile since Month is an enum and enums cannot be compared to an int
- Month.JANUARY.getValue() returns 1 because getValue is implemented as
return ordinal() + 1
- ZonedDateTimes can be created from a LocalDate, LocalTime and a ZoneId:
LocalDate date = LocalDate.of(2018, Month.JANUARY, 31); LocalTime time = LocalTime.of(15, 30); LocalDateTime dateTime = LocalDateTime.of(date, time); ZoneId zone = ZoneId.of("Europe/Paris"); ZonedDateTime zonedDateTime = ZonedDateTime.of(date, time, zone);
- You should not chain Period creation like
Period.ofYears(2).ofWeeks(1);
This will return P1D and you will not have your two years. System.out.println(Period.ofWeeks(2));
will print out P14D cause period is in YMD format.- Period is a day or more of time
- Duration is for units of time smaller than a day
- Period notation starts with P and Duration notation starts with PT
- Instant represents a specific moment (GMT)
- You can convert a ZonedDateTime to Instant with toInstant()
- The maximum Unit of Time you can use in the plus method of Instant is ChronoUnit.DAYS because Java implemented the plus method as:
public Instant plus(long amountToAdd, TemporalUnit unit) { if (unit instanceof ChronoUnit) { switch ((ChronoUnit) unit) { case NANOS: return plusNanos(amountToAdd); case MICROS: return plus(amountToAdd / 1000_000, (amountToAdd % 1000_000) * 1000); case MILLIS: return plusMillis(amountToAdd); case SECONDS: return plusSeconds(amountToAdd); case MINUTES: return plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_MINUTE)); case HOURS: return plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_HOUR)); case HALF_DAYS: return plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_DAY / 2)); case DAYS: return plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_DAY)); } throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); } return unit.addTo(this, amountToAdd); }
- Java accounts for Daylight saving time
- When constantly updating the value of a String it might be better to use StringBuilder since String is immutable
- If you need a threadsafe StringBuilder use StringBuffer instead
- Locale can be created in different ways
Locale.FRANCE; new Locale("fr"); new Locale("fr_CA"); new Locale.Builder() .setLanguage("fr") .setRegion("CA") .build();
- A resource bundle contains the local specific values that are used in your application
- You can specify a default value when fetching a property from a resource bunde with getString()
- You cannot do that when using get()
- Resource files for a Shop program can be named like Shop_fr_FR.java, Shop_fr_FR.properties, Shop_fr.java, Shop_fr.properties … Shop.properties
- The order above is also the order in which java will search through the different resource files
- When using a NumberFormatter you use format() to turn a number into a String and parse() to turn a String into a number
-
LocalDateTime ldt = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT); // both of the following lines produce the same result System.out.println(formatter.format(ldt)); System.out.println(ldt.format(formatter));
Code Examples: https://github.com/Nxtra/OCP-Examples/tree/master/5_DatesStringsLocalization