Did you just set up your environment to start working with java 8? Jump right to the chapter of lambda or ran across just about every blog that gives you examples? There is more to java 8 than just lambdas!
If you have worked with java dates you have pulled your hair while using
java.util.GregorianCalendar. If you ran across joda time library most likely you are using it. If not, luckily with java 8 and the development of JSR 310: Date and Time API it will easier to use date time methods. The goal was Date-Time API clarity in method constructs, cleaner readable code, immutable and extensibility. Oh yea, we even got Day of the week and month enums!
High level classes in API
For the most part the date time trail does a good job describing the new classes, below are a few to focus on first:
|Representing a date without a time
|Handles both date and time
|Is a date based amount of time in terms of years
|Measures time in seconds or nano seconds
|Defines units used to measure time. For example, if you need to find the number of days between two dates, ChronoUnit.DAYS.between(localdate1, localdate2)
|Formatter for printing and parsing date-time objects
Moving from JODA to Java 8 date-time API
While working on updating java date examples the API is clean. Converting from JODA to java 8 date time API should natural switch while converting from legacy java date-time will be a slight learning curve. Here is a high level comparison between joda and the new java 8 date time api:
|Java 8 date-time API
|Checks if a date is after the specified date
|hecks if a date is before the specified date
|Years.between, Weeks.weeksBetween, Seconds.secondsBetween, etc...
|ChronoUnit.DAYS.between, Period.between, Duration.between
|Calculates the amount of time between two date objects. For ChronoUnit subsitute DAYS, YEARS, etc
|Year.of(2007).isLeap(), LocalDate.ofYearDay(2007, 1).isLeapYear()
|Checks if the year is a leap year
|Convert text string to a date
|java.time.format.DateTimeFormatter LocalDate.format, LocalDateTime.format
|Formats date to a specified string format.
|Subtract specified number of months, days, year
|DateTime.plusDays, DateTime.plusWeeks, etc
|LocalDateTime.plusDays, LocalDateTime.plusHours, etc
|Add specified number of months, days, year
|Get current date time
At first misunderstood
UnsupportedTemporalTypeException indicates that a ChronoField or ChronoUnit is not supported for a Temporal class. I ran across this error while updating the number of hours between two dates. Thinking through the example below, I wanted to get today, subtract 1 day and then find the number hours between the dates. This resulted in
LocalDate only contains year, month and day fields not hours. It makes sense now but be aware when trying to access fields on temporal objects.
Using Period to find hours
While working through hours between dates example. I started by using
Period but period is represented by all three units together: months, days, and years. So lets say I wanted to find the difference between my birthday and today. Using period would result in output that could look like 53 years, 4 months, and 29. When you want to find a single unit of time use
The hour example using
I updated all the date time examples to use Java 8 date time api. Below are just a few: