Tuesday, May 9, 2017

Java 8 Date and Time API: LocalDate


Java 8 date and time API introduces a new class LocalDate under java.time package which represents a local date without a timezone. LocalDate instances are immutable (cannot be changed) so any operations performed on LocalDate will always return a new instance.

In this tutorial, you will learn:
  • How to create a LocalDate instance representing the current date
  • How to create a LocalDate instance from a given year, month and day
  • How to access the LocalDate properties (e.g. month, year, day of week, etc.)
  • How to perform plus and minus operations on LocalDate
  • How to use TemporalAdjusters to adjust LocalDate
Example

package com.melody.datetime;

import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.TemporalAdjusters;

public class LocalDateDemo {

 public static void main(String[] args) {
  LocalDate currentLocalDate = LocalDate.now();
  System.out.println("Current local date: " + currentLocalDate);

  // Create a LocalDate instance from a given year, month and day
  LocalDate localDate = LocalDate.of(2017, 11, 14);
  System.out.println("Local date from a given year, month & day: " + localDate);

  // Create a LocalDate instance from a given year, MONTH (enum) and day
  localDate = LocalDate.of(2017, Month.NOVEMBER, 14);
  System.out.println("Local date from a given year, MONTH (enum) & day: " + localDate);

  // Access the LocalDate properties
  System.out.println("Get year: " + localDate.getYear());
  System.out.println("Get month (enum): " + localDate.getMonth());
  System.out.println("Get month: " + localDate.getMonthValue());
  System.out.println("Get day of month: " + localDate.getDayOfMonth());
  System.out.println("Get day of year: " + localDate.getDayOfYear());
  System.out.println("Get day of week: " + localDate.getDayOfWeek());
  System.out.println("Get length of month: " + localDate.lengthOfMonth());
  System.out.println("Get length of year: " + localDate.lengthOfYear());
  System.out.println("Is leap year: " + localDate.isLeapYear());

  // Add and minus operations on LocalDate
  System.out.println("Add 1 yr, 1 month & 1 day to 2017-11-14: " + localDate.plusYears(1).plusMonths(1).plusDays(1));
  System.out.println("Minus 1 yr, 1 month & 1 day from 2017-11-14: " + localDate.minusYears(1).minusMonths(1).minusDays(1));

  // Adjust the LocalDate
  System.out.println("Adjust 2017-11-14 into 2019-12-14: " + localDate.withYear(2019).withMonth(12));

  // Adjust the LocalDate using TemporalAdjusters
  System.out.println("Adjust 2017-11-14 into first day of the month: " + localDate.with(TemporalAdjusters.firstDayOfMonth()));
 }
}

Output

Current local date: 2017-05-09
Local date from a given year, month & day: 2017-11-14
Local date from a given year, MONTH (enum) & day: 2017-11-14
Get year: 2017
Get month (enum): NOVEMBER
Get month: 11
Get day of month: 14
Get day of year: 318
Get day of week: TUESDAY
Get length of month: 30
Get length of year: 365
Is leap year: false
Add 1 yr, 1 month & 1 day to 2017-11-14: 2018-12-15
Minus 1 yr, 1 month & 1 day from 2017-11-14: 2016-10-13
Adjust 2017-11-14 into 2019-12-14: 2019-12-14
Adjust 2017-11-14 into first day of the month: 2017-11-01


No comments:

Post a Comment