Java 8 - LocalDate example




The java.time.LocalDate class is an immutable class which represents a date without time-zone information, such as 2017-05-15.

This class does not store and represent a time or time-zone. Instead, it is a description of the date which can be used for birthday or holiday.

The following examples are provided to demonstrate the usage of the LocalDate in java program.

 

How to create a LocalDate in Java 8

You can use the LocalDate.now() and LocalDate.of() methods to create the current date and specific date object respectively.

package com.boraji.tutorial.datetime;

import java.time.LocalDate;
import java.time.Month;

public class LocalDateExample {
   public static void main(String[] args) {

      // Current date
      LocalDate localDate = LocalDate.now();
      System.out.println(localDate);

      //Specific date
      LocalDate localDate2 = LocalDate.of(2017, Month.MAY, 15);
      System.out.println(localDate2);
   }
}

Output

2017-05-14
2017-05-15

 

How to get year, month, day from LocalDate

You can use the getYear(), getMonth() and getDayOfMonth() methods of the LocalDate class to get year, month and day respectively.

package com.boraji.tutorial.datetime;

import java.time.LocalDate;

public class LocalDateExample {
   public static void main(String[] args) {
      LocalDate localDate = LocalDate.now();
      System.out.println("Year : " + localDate.getYear());
      System.out.println("Month : " + localDate.getMonth().getValue());
      System.out.println("Day of Month : " + localDate.getDayOfMonth());
      System.out.println("Day of Week : " + localDate.getDayOfWeek());
      System.out.println("Day of Year : " + localDate.getDayOfYear());
   }
}

Output

Year : 2017
Month : 5
Day of Month : 14
Day of Week : SUNDAY
Day of Year : 134

 

How to add or subtract days, months, weeks and years to LocalDate

You can use the plus and minus methods of the LocalDate class to add or subtract days, months etc.

package com.boraji.tutorial.datetime;

import java.time.LocalDate;

public class LocalDateExample {
   public static void main(String[] args) {

      LocalDate localDate = LocalDate.now();

      // LocalDate's plus methods 
      System.out.println("Addition of days : " + localDate.plusDays(5));
      System.out.println("Addition of months : " + localDate.plusMonths(15));
      System.out.println("Addition of weeks : " + localDate.plusWeeks(45));
      System.out.println("Addition of years : " + localDate.plusYears(5));

      // LocalDate's minus methods 
      System.out.println("Subtraction of days : " + localDate.minusDays(5));
      System.out.println("Subtraction of months : " + localDate.minusMonths(15));
      System.out.println("Subtraction of weeks : " + localDate.minusWeeks(45));
      System.out.println("Subtraction of years : " + localDate.minusYears(5));
   }
}

Output

Addition of days : 2017-05-19
Addition of months : 2018-08-14
Addition of weeks : 2018-03-25
Addition of years : 2022-05-14
Subtraction of days : 2017-05-09
Subtraction of months : 2016-02-14
Subtraction of weeks : 2016-07-03
Subtraction of years : 2012-05-14

 

How to compare LocalDate objects in Java

You can use the isEqual(), isAfter() and isBefore() methods of the LocalDate class to compare the LocalDate objects.

package com.boraji.tutorial.datetime;

import java.time.LocalDate;
import java.time.Month;

public class LocalDateExample {
   public static void main(String[] args) {

      LocalDate localDate1 = LocalDate.now();
      LocalDate localDate2 = LocalDate.of(2017, Month.MAY, 14);
      LocalDate localDate3 = LocalDate.of(2016, Month.MAY, 15);

      // isEqual() example
      if (localDate1.isEqual(localDate2)) {
         System.out.println("localDate1 and localDate2 are equal");
      } else {
         System.out.println("localDate1 and localDate2 are not equal");
      }

      // ifAfter() example
      if (localDate2.isAfter(localDate3)) {
         System.out.println("localDate2 comes after localDate3");
      }
      
      // isBefore() example
      if (localDate3.isBefore(localDate1)) {
         System.out.println("localDate3 comes before localDate1");
      }
   }
}

Output

localDate1 and localDate2 are equal
localDate2 comes after localDate3
localDate3 comes before localDate1

 

How to get number of days in a month or year using LocalDate

You can use the lengthOfMonth() and lengthOfYear() methods of the LocalDate class to count the number of days in a month and year respectively.

package com.boraji.tutorial.datetime;

import java.time.LocalDate;
import java.time.Month;

public class LocalDateExample {
   public static void main(String[] args) {

      LocalDate localDate1 = LocalDate.of(2017, Month.JANUARY, 1);
      LocalDate localDate2 = LocalDate.of(2016, Month.FEBRUARY, 1);

      // Number of days in a month
      System.out.println("Number of days in " + localDate1.getMonth() + " : " 
            + localDate1.lengthOfMonth());
      System.out.println("Number of days in " + localDate2.getMonth() + " : " 
            + localDate2.lengthOfMonth());

      // Number of days in a year
      System.out.println("Number of days in " + localDate1.getYear() + " : "
            +localDate1.lengthOfYear());
      System.out.println("Number of days in " + localDate2.getYear() + " : " 
            +localDate2.lengthOfYear());
   }
}

Output

Number of days in JANUARY : 31
Number of days in FEBRUARY : 29
Number of days in 2017 : 365
Number of days in 2016 : 366

 

How to check if a given year is leap year or not using LocalDate

You can use the isLeapYear() method of the LocalDate class to check whether a given year is leap year or not.

package com.boraji.tutorial.datetime;

import java.time.LocalDate;
import java.time.Month;

public class LocalDateExample {
   public static void main(String[] args) {

      LocalDate localDate1 = LocalDate.of(2017, Month.JANUARY, 1);
      LocalDate localDate2 = LocalDate.of(2016, Month.JANUARY, 1);

      if(localDate1.isLeapYear()){
         System.out.println(localDate1.getYear()+ " is a leap year");
      }else{
         System.out.println(localDate1.getYear()+ " is not a leap year");
      }
      
      if(localDate2.isLeapYear()){
         System.out.println(localDate2.getYear()+ " is a leap year");
      }else{
         System.out.println(localDate2.getYear()+ " is not a leap year");
      }
   }
}

Output

2017 is not a leap year
2016 is a leap year

 

How to convert string to LocalDate in java

You can use the parse() method of the LocalDate class to convert the String to LocalDate .

package com.boraji.tutorial.datetime;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class LocalDateExample {
   public static void main(String[] args) {

      // ISO Date
      LocalDate localDate = LocalDate.parse("2017-05-03",
            DateTimeFormatter.ISO_LOCAL_DATE);
      System.out.println(localDate);

      // yyyy/MM/dd pattern
      LocalDate localDate1 = LocalDate.parse("2017/05/12",
            DateTimeFormatter.ofPattern("yyyy/MM/dd"));
      System.out.println(localDate1);

      // MMM dd, yyyy pattern
      LocalDate localDate2 = LocalDate.parse("May 05, 2017",
            DateTimeFormatter.ofPattern("MMM dd, yyyy"));
      System.out.println(localDate2);

      // dd-MMM-yyyy pattern
      LocalDate localDate3 = LocalDate.parse("12-Jan-2017",
            DateTimeFormatter.ofPattern("dd-MMM-yyyy"));
      System.out.println(localDate3);

      // dd-LL-yyyy pattern
      LocalDate localDate4 = LocalDate.parse("12-01-2017",
            DateTimeFormatter.ofPattern("dd-LL-yyyy"));
      System.out.println(localDate4);

   }
}

Output

2017-05-03
2017-05-12
2017-05-05
2017-01-12
2017-01-12

 

How to convert LocalDate to String in java

You can use the format() method of the LocalDate class to convert the LocalDate to String.

package com.boraji.tutorial.datetime;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class LocalDateExample {
   public static void main(String[] args) {

      // ISO Date
      LocalDate localDate = LocalDate.now();
      DateTimeFormatter dateFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
      System.out.println(localDate.format(dateFormatter));

      // yyyy/MM/dd pattern
      DateTimeFormatter dateFormatter1 = DateTimeFormatter
            .ofPattern("yyyy/MM/dd");
      System.out.println(localDate.format(dateFormatter1));

      // MMMM dd, yyyy pattern
      DateTimeFormatter dateFormatter2 = DateTimeFormatter
            .ofPattern("MMMM dd, yyyy");
      System.out.println(localDate.format(dateFormatter2));

      // dd-MMM-yyyy pattern
      DateTimeFormatter dateFormatter3 = DateTimeFormatter
            .ofPattern("dd-MMM-yyyy");
      System.out.println(localDate.format(dateFormatter3));
      
      // dd-LL-yyyy pattern
      DateTimeFormatter dateFormatter4 = DateTimeFormatter
            .ofPattern("dd-LL-yyyy");
      System.out.println(localDate.format(dateFormatter4));

   }
}

Output

2017-05-29
2017/05/29
May 29, 2017
29-May-2017
29-05-2017