Java Date Time API QuickStart

The authors and designers of Java 8, with Stephen Colebourne (author:the Joda-Time library), under JSR 310, have finally delivered us the native Java Date Time API we always wanted, but never had. For those of you familiar with the Sonar violations when copying/assigning the erstwhile java.util.Date, all the core classes in the new API are immutable. Here are my key takeaways:

  1. Object Construction using Factory Methods -  of, from, which
  2. Immutable Core Classes and Thread-Safe Formatters -  LocalDate, LocalTime   
  3. Adjusters to adjust the duration of an available local date object -  TemporalAdjusters.firstDayOfMonth()
  4. Simplistic Method Chaining now possible because of immutable behavior and each factory returning a new object. LocalDate.now().withMonth( 1).with(TemporalAdjusters. lastDayOfYear());
  5. Classes for Time Zones (ZoneId.of("America/Chicago")), Periods (days, months, years), Durations (seconds, nanoseconds, etc.) and Chronologies (support non-ISO calendaring systems)

Here are some rudimentary business date operations we can perform using the new API. You can also download the source code for all of the following examples from this library being managed at GitHub.


Find Current Date

Use the LocalDate.now() method to obtain the current date from the system clock in the default time-zone

Read more

Find Tomorrow's Date

In order to get tomorrow's date, first find the current date, and then add one day to it

Read more

Find Yesterday's Date

In order to get yesterday's date, first find the current date, and then subtract one day from it

Read more

Finding Nth Weekday Of Month and Year

Sometimes, businesses require to find the Nth Weekday in a given month. E.g. say a school wishes to know Dr. Martin Luther King, Jr. Day

Read more

Find Last Day of Next Month

In order to get the last date of the next month, we first find the current date, and then we adjust the date using the strategy we need here.

Read more

Finding Nth Weekday Of current Month

Using TemporalAdjusters to find a date given the Nth DayOfWeek. E.g. Finding the 4th Thursday of November

Read more

Find Last Day of Current Month

In order to get the last date of the current month, we first find the current date, and then we adjust the date

Read more

Find First Day of Current Month

In order to get the start date of the current month, we first find the current date, and then we adjust the date

Read more

Find Day After Tomorrow's Date

In order to get day-after-tomorrow's date, first find the current date, and then add two days to it

Read more

Finding First Day of Next Month

In order to get the start date of the next month, we first find the current date, and then we adjust

Read more