Java LocalDate

Filed Under: Java

Java LocalDate class is part of Java 8 Date API.

Java LocalDate

  • Java LocalDate is immutable class and hence thread safe.
  • LocalDate provides date output in the format of YYYY-MM-dd.
    Java LocalDate
  • The LocalDate class has no time or Timezone data. So LocalDate is suitable to represent dates such as Birthday, National Holiday etc.
  • LocalDate class implements Temporal, TemporalAdjuster, ChronoLocalDate and Serializable interfaces.
  • LocalDate is a final class, so we can’t extend it.
  • LocalDate is a value based class, so we should use equals() method for comparing if two LocalDate instances are equal or not.

Importing Java LocalDate Class

Java LocalDate class is in java.time package. So you can import it using import statement:


import java.time.LocalDate;

Creating LocalDate instance

There are four standard ways to create LocalDate instance.

  1. Invoking the static now() method that returns the current date from system clock.
    
    LocalDate ldObj = LocalDate.now();
    		
    System.out.println(ldObj); //prints "2017-11-10"
    
  2. By passing Year, Month, and Day values to LocalDate of() method
    
    LocalDate ldObj = LocalDate.of(2017, 11, 6);
    		
    System.out.println(ldObj); //prints "2017-11-06"
    
  3. Using LocalDate parse() method.
    
    LocalDate ld = LocalDate.parse("2017-11-10");
    
  4. Using LocalDate ofInstant() method as shown below.
    
    LocalDate ld = LocalDate.ofInstant(Instant.now(), ZoneId.systemDefault());
    

Retrieving Date Information from LocalDate Class

Java LocalDate class provides a lot of useful methods to get the more details about date, for example year, month, day of year etc. Let’s look into these through some example programs.

  1. getYear(): returns the year contained within the LocalDate object.
    
    LocalDate ld = LocalDate.of(2017, 11, 10);
    		
    int year = ld.getYear();
    		
    System.out.println(year); //prints "2017"
    
  2. getMonth(): returns the month contained within the LocalDate object.
    
    LocalDate ld = LocalDate.now(); // today is 10-Nov-2017
    
    Month month = ld.getMonth();
    
    System.out.println(month + "," + month.getValue()); // prints "NOVEMBER,11"
    
  3. getDayOfMonth(): returns the day of the month contained within the LocalDate object.
    
    LocalDate ld = LocalDate.now(); // today is 10-Nov-2017
    
    int dayOfMonth = ld.getDayOfMonth();
    
    System.out.println(dayOfMonth); // prints "10"
    
  4. getDayOfWeek(): returns the day of the week from the LocalDate object.
    
    LocalDate ld = LocalDate.now(); // today is 10-Nov-2017
    
    DayOfWeek dayOfWeek = ld.getDayOfWeek();
    
    System.out.println(dayOfWeek + "," + dayOfWeek.getValue()); // prints "FRIDAY,5"
    
  5. getDayOfYear(): returns the day of the year from the LocalDate instance.
    
    LocalDate ld = LocalDate.of(2017, 2, 10);
    
    int dayOfYear = ld.getDayOfYear();
    
    System.out.println(dayOfYear); // prints "41" i.e. 31+10
    

Java LocalDate methods – plus and minus

  1. plusYears(long yearsToAdd): adds the yearsToAdd to the year value and return the copy of LocalDate object. Since LocalDate is immutable, the specified object doesn’t change. Also if the new date is invalid then the last valid date is returned.
    
    LocalDate ld = LocalDate.of(2017, 2, 10);
    
    LocalDate ldNew = ld.plusYears(3);
    
    System.out.println(ldNew + ":" + ld); // prints "2020-02-10:2017-02-10"
    
    ld = LocalDate.of(2016, 2, 29);
    ldNew = ld.plusYears(1); // 2017-02-29 is invalid date, so it will return 2017-02-28
    System.out.println(ldNew); // prints "2017-02-28"
    
  2. plusWeeks(long weeksToAdd): returns a new LocalDate instance after adding weeksToAdd weeks, no change in the specified object.
    
    LocalDate ld = LocalDate.of(2017, 11, 10);
    
    LocalDate ldNew = ld.plusWeeks(3);
    
    System.out.println(ldNew); // 2017-12-01
    
  3. plusMonths(long monthsToAdd): returns a copy of specified LocalDate object after adding monthsToAdd months.
    
    LocalDate ld = LocalDate.of(2017, 11, 10);
    
    LocalDate ldNew = ld.plusMonths(3);
    
    System.out.println(ldNew); // 2018-02-10
    
  4. plusDays(long daysToAdd): returns a copy of specified LocalDate object after adding daysToAdd days.
    
    LocalDate ld = LocalDate.of(2017, 11, 10);
    
    LocalDate ldNew = ld.plusDays(10);
    
    System.out.println(ldNew); // 2017-11-20
    
  5. minusDays(long daysToSubtract): returns a copy of specified LocalDate object after subtracting daysToSubtract days.
    
    LocalDate ld = LocalDate.of(2017, 11, 10);
    
    LocalDate ldNew = ld.minusDays(10);
    
    System.out.println(ldNew); // 2017-10-31
    
  6. minusWeeks(long weeksToSubtract): returns a copy of specified LocalDate object after subtracting weeksToSubtract weeks.
    
    LocalDate ld = LocalDate.of(2017, 11, 10);
    
    LocalDate ldNew = ld.minusWeeks(1);
    
    System.out.println(ldNew); // 2017-11-03
    
  7. minusMonths(long monthsToSubtract): returns a copy of specified LocalDate object after subtracting monthsToSubtract months.
    
    LocalDate ld = LocalDate.of(2017, 11, 10);
    
    LocalDate ldNew = ld.minusMonths(1);
    
    System.out.println(ldNew); // 2017-10-10
    
  8. minusYears(long yearsToSubtract): returns a copy of specified LocalDate object after subtracting yearsToSubtract years. If the new LocalDate is invalid (29-Feb), then last valid date is returned.
    
    LocalDate ld = LocalDate.of(2016, 2, 29);
    
    LocalDate ldNew = ld.minusYears(2);
    
    System.out.println(ldNew); // 2014-02-28
    

Java LocalDate to Date

We should avoid using legacy java.util.Date class but sometimes we have to convert LocalDate to Date for legacy support. Below are two ways to convert LocalDate to Date, you can use any of them.


package com.journaldev.java;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

public class JavaLocalDateExample {

	public static void main(String args[]) {

		LocalDate ld = LocalDate.now();

		Date date = Date.from(ld.atStartOfDay(ZoneId.systemDefault()).toInstant());

		System.out.println(date); // prints "Fri Nov 10 00:00:00 IST 2017"
		
		Date date1 = java.sql.Date.valueOf(ld);
		
		System.out.println(date1); // prints "2017-11-10"
	}
}

Java LocalDate to Date

That’s all for java LocalDate class.

References: API Doc, StackOverflow Article

Comments

  1. Valentino says:

    There’s no LocalDate,ofInstant method in the Api, so compilation fails.
    Maybe you meant getting the LocalDate from LocalDateTime?

    LocalDateTime ldt = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());
    System.out.println(ldt.toLocalDate());

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages