Java 8 Date Classes with Examples

The Date-Time API provides four classes that deal exclusively with date information, without respect to time or time zone.
  1. LocalDate
  2. YearMonth
  3. MonthDay
  4. Year
Let's discuss each class with examples. Examples of this guide are available on Github.

1. LocalDate

A LocalDate represents a year-month-day in the ISO calendar and is useful for representing a date without a time. You might use a LocalDate to track a significant event, such as a birth date or wedding date.

1.2 LocalDate class Examples

Example 1: Get local date by passing year,month and dayOfMonth to this method.
LocalDate getLocalDateUsingFactoryOfMethod(int year, int month, int dayOfMonth) {
 return LocalDate.of(year, month, dayOfMonth);
}
Example 2: Get current date or today's using this method.
LocalDate getLocalDateFromClock() {
 LocalDate localDate = LocalDate.now();
 return localDate;
}
Example 3: Get next day of today's or specific date using this method.
LocalDate getNextDay(LocalDate localDate) {
 return localDate.plusDays(1);
}
Example 4: Get previous day by passing specific date to this method.
LocalDate getPreviousDay(LocalDate localDate) {
 return localDate.minus(1, ChronoUnit.DAYS);
}
Example 5: Get day of week using this method.
DayOfWeek getDayOfWeek(LocalDate localDate) {
 DayOfWeek day = localDate.getDayOfWeek();
 return day;
}
Example 6: Get first day of the month.
LocalDate getFirstDayOfMonth() {
 LocalDate firstDayOfMonth = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
 return firstDayOfMonth;
}
Example 7: Get start of the day.
LocalDateTime getStartOfDay(LocalDate localDate) {
 LocalDateTime startofDay = localDate.atStartOfDay();
 return startofDay;
}
Example 8: check for recurring events e.g. birthday in Java 8.
public static void recurringDate(LocalDate today) {
 LocalDate dateOfBirth = LocalDate.of(2010, 01, 14);
 MonthDay birthday = MonthDay.of(dateOfBirth.getMonth(), dateOfBirth.getDayOfMonth());
 MonthDay currentMonthDay = MonthDay.from(today);
 if (currentMonthDay.equals(birthday)) {
  System.out.println("Many Many happy returns of the day !!");
 } else {
  System.out.println("Sorry, today is not your birthday");
 }
}
Let's see few examples of how to format dates.
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy");
private static final DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("d-MMM-yyyy");
private static final DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("d/MM/yyyy");
public static void main(String[] args) {
 //default format
 System.out.println("Default format of LocalDate = " + LocalDate.now());
   
 // The ISO date formatter that formats or parses a date without an
  // offset, such as '20111203'
 LocalDate date = LocalDate.now();
 
 System.out.println(date.format(DateTimeFormatter.BASIC_ISO_DATE));
 
 System.out.println(date.format(DateTimeFormatter.ISO_DATE));
 
 System.out.println(formatter.format(LocalDate.parse("16/08/2016", formatter)));
 
 System.out.println(formatter1.format(LocalDate.parse("16-Aug-2016", formatter1)));
 
 System.out.println(formatter2.format(LocalDate.parse("16/08/2016", formatter2)));
}
Output:
Default format of LocalDate = 2018-07-11
20180711
2018-07-11
16/08/2016
16-Aug-2016
16/08/2016

2. YearMonth

The YearMonth class represents the month of a specific year. The following example uses the YearMonth.lengthOfMonth() method to determine the number of days for several year and month combinations.
YearMonth date = YearMonth.now();
System.out.printf("%s: %d%n", date, date.lengthOfMonth());

YearMonth date2 = YearMonth.of(2010, Month.FEBRUARY);
System.out.printf("%s: %d%n", date2, date2.lengthOfMonth());

YearMonth date3 = YearMonth.of(2012, Month.FEBRUARY);
System.out.printf("%s: %d%n", date3, date3.lengthOfMonth());
The output from this code looks like the following:
2013-06: 30
2010-02: 28
2012-02: 29

3. MonthDay

The MonthDay class represents the day of a particular month, such as New Year's Day on January 1.
The following example uses the MonthDay.isValidYear method to determine if February 29 is valid for the year 2010. The call returns false, confirming that 2010 is not a leap year.
MonthDay date = MonthDay.of(Month.FEBRUARY, 29);
boolean validLeapYear = date.isValidYear(2010);

4. Year

The Year class represents a year. The following example uses the Year.isLeap method to determine if the given year is a leap year. The call returns true, confirming that 2012 is a leap year.
boolean validLeapYear = Year.of(2012).isLeap();

Comments