Pages

Friday, January 1, 2021

Java 8 LocalDate Class With Examples

1. Overview

In this tutorial, you'll learn how to use LocalDate class in java 8 Date Time API and its methods with example programs.

In java 8, LocalDate class is an immutable class to represent the date with the default format of "yyyy-MM-dd".

And LocalDate does not represent the time part of date or timezone.

This is mainly used to store the date of birth or date of join because this object works with only with year, month and day but not with the hours, minutes and seconds.

Java 8 LocalDate Class With Examples


2. Java 8 LocalDate Class

This class is defined as final class in the api and implements Temporal, TemporalAdjuster, ChronoLocalDate, Serializable interfaces.

How to get the current date and time in java 8?

LocalDate class has many useful methods which reduces the manual work to write the date utility methods.

below are the few methods which are frequently used by the developers.

LocalDate of(int year, int month, int dayOfMonth): Creates a new LocalDate object with the given date parameters.

LocalDateTime atTime(int hour, int minute): It is used to combine this date with a time to create a LocalDateTime.

int compareTo(ChronoLocalDate other): It is used to compares this date to another date.

boolean equals(Object obj): It is used to check if this date is equal to another date.

String format(DateTimeFormatter formatter): It is used to format this date using the specified formatter.

int get(TemporalField field): It is used to get the value of the specified field from this date as an int.

boolean isLeapYear(): It is used to check if the year is a leap year, according to the ISO proleptic calendar system rules.

LocalDate minusDays(long daysToSubtract): It is used to return a copy of this LocalDate with the specified number of days subtracted.

LocalDate minusMonths(long monthsToSubtract): It is used to return a copy of this LocalDate with the specified number of months subtracted.

static LocalDate now(): It is used to obtain the current date from the system clock in the default time-zone.

LocalDate plusDays(long daysToAdd): It is used to return a copy of this LocalDate with the specified number of days added.

LocalDate plusMonths(long monthsToAdd): It is used to return a copy of this LocalDate with the specified number of months added.


3. LocalDate Examples

Next, let us see the how to use these methods to work with dates such as adding days, comparing the dates, checking the leap year. etc.

All methods of LocalDate class recreates the new object the modified data.

To see the contents of LocalDate object, write the object onto the log or console. Because, toString() method is overridden in this class and it returns the date in the redable format "yyyy-MM-dd"

3.1 Example 1: Create Objects for LocalDate

LocalDate has 2 methods to create the date objects and those are now() and of() methods.

package com.javaprogramto.java8.dates.localdate;

import java.time.LocalDate;

public class LocalDateCreation {

	public static void main(String[] args) {

		// Dates creation with now() and of() methods.
		LocalDate currentDate = LocalDate.now();
		LocalDate futureDate = LocalDate.of(2025, 10, 10);

		// printing the dates.
		System.out.println("Current date : " + currentDate);
		System.out.println("Future date : " + futureDate);

	}

} 

Output:

Current date : 2020-11-23
Future date : 2025-10-10
 

3.2 Example 2:  Adding days and getting next, previous days

Use plusDays() and minusDays() methods to get the next or previous days.

And you go add or go back to a particular day using these two methods.

package com.javaprogramto.java8.dates.localdate;

import java.time.LocalDate;

public class AddingDaysExample {

	public static void main(String[] args) {
		// creating current date time
		LocalDate currentDate = LocalDate.now();
		
		// Getting the next date
		LocalDate nextDay = currentDate.plusDays(1);
		
		// Getting the previous date
		LocalDate previousDay = currentDate.minusDays(1);
		
		System.out.println("Current date : "+currentDate);
		System.out.println("Next date : "+nextDay);
		System.out.println("Previous date : "+previousDay);
	}
}
 

Output:

Current date : 2020-11-23
Next date : 2020-11-24
Previous date : 2020-11-22
 

3.3 Example 3:  Checking Leap Year

Call isLeapYear() method from LocalDate class. This method returns boolean as true if the year is leap year else false.

package com.javaprogramto.java8.dates.localdate;

import java.time.LocalDate;

public class LeapYearExample {
	public static void main(String[] args) {
		//	Example 1 - leap year checking
		LocalDate date1 = LocalDate.now();
		boolean isLeapYear = date1.isLeapYear();
		System.out.println(date1 + " is leap year ? " + isLeapYear);

		//	Example 2
		LocalDate date2 = LocalDate.of(2000, 01, 01);
		isLeapYear = date2.isLeapYear();
		System.out.println(date2 + " is leap year ? " + isLeapYear);

		//	Example 3
		LocalDate date3 = LocalDate.of(2006, 01, 01);
		isLeapYear = date3.isLeapYear();
		System.out.println(date3 + " is leap year ? " + isLeapYear);
	}
}

 

Output:

2020-11-23 is leap year ? true
2000-01-01 is leap year ? true
2006-01-01 is leap year ? false

 

3.4 Example 4: Convert LocalDate to String

Use format() method to convert LocalDate to String in java 8.

package com.javaprogramto.java8.dates.localdate;

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

public class LocalDateToStringExample {

	public static void main(String[] args) {

		// Converting LocalDate to String
		// Example 1
		LocalDate date1 = LocalDate.now();
		String date1Str = date1.format(DateTimeFormatter.ISO_DATE);
		System.out.println("Date1 in string :  " + date1Str);

		// Example 2
		LocalDate date2 = LocalDate.of(2000, 01, 01);
		String date2Str = date2.format(DateTimeFormatter.ISO_DATE);
		System.out.println("Date2 in string :  " + date2Str);

		// Example 3
		LocalDate date3 = LocalDate.of(2006, 01, 01);
		String date3Str = date3.format(DateTimeFormatter.ISO_DATE);
		System.out.println("Date3 in string :  " + date3Str);
	}
}
 

Output:

Date1 in string :  2020-11-23
Date2 in string :  2000-01-01
Date3 in string :  2006-01-01
 

3.5 Example 5: Convert String To LocalDate

Use parse() method to convert String to LocalDate.

package com.javaprogramto.java8.dates.localdate;

import java.time.LocalDate;

// String to LocalDate in java 8
public class StringToLocalDateExample {

	public static void main(String[] args) {

		// Example 1
		String dateInStr = "2021-01-01";
		LocalDate date1 = LocalDate.parse(dateInStr);
		System.out.println("String to LocalDate : " + date1);

		// Example 2
		String dateInStr2 = "2025-11-30";
		LocalDate date2 = LocalDate.parse(dateInStr2);
		System.out.println("String to LocalDate : " + date2);

	}

}
 

3.6 Example 6: Convert LocalDate to LocalDateTime

Use atTime() method to convert LocalDate to LocalDateTime.

package com.javaprogramto.java8.dates.localdate;

import java.time.LocalDate;
import java.time.LocalDateTime;

public class LocalDateAtTimeExample {

	public static void main(String[] args) {
		// localdate 
		LocalDate localDate = LocalDate.now();
		
		int hour = 10;
		int minute = 20;
		int second = 30;
		
		// setting time details and converting LocalDate to LocalDateTime.
		LocalDateTime dateAndTime = localDate.atTime(hour, minute, second);
		
		System.out.println("LocalDate : "+localDate);
		System.out.println("LocalDateTime : "+dateAndTime);

	}

}
 

Output:

LocalDate : 2020-11-23
LocalDateTime : 2020-11-23T10:20:30
 


4. Conclusion

In this article, you have seen What is LocalDate class in java 8 and how to LocalDate class methods with examples.

GitHub

LocalDateCreation

AddingDaysExample

LeapYearExample

LocalDateAtTimeExample

LocalDateToStringExample

StringToLocalDateExample

Read Next:

How to convert Calendar to LocalDate ?

How to compare two dates in java 8?

How to add days to current time in java 8?

LocalDate

No comments:

Post a Comment

Please do not add any spam links in the comments section.