$show=/label

Java 8 LocalDate Class With Examples

SHARE:

A quick guide to work with the java 8 LocalDate class. Examples to use LocalDate methods.

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

COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,17,Arrays,16,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,5,Collections,29,Collector,1,Command Line,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,93,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,35,Dictionary,1,Difference,1,Download,1,Eclipse,2,Efficiently,1,Error,1,Errors,1,Exception,1,Exceptions,3,Fast,1,Files,14,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,4,Grant,1,Grep,1,HashMap,1,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,5,Iterate,2,Jackson API,3,Java,30,Java 10,1,Java 11,5,Java 12,5,Java 13,2,Java 14,2,Java 8,111,Java 8 Difference,2,Java 8 Stream Conversions,2,java 8 Stream Examples,3,Java 9,1,Java Conversions,11,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,105,Java Spark,1,java.lang,5,java.util. function,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,1,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,2,Math,1,Matrix,5,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,28,String,59,String Programs,12,String Revese,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,16,Unix,3,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Java 8 LocalDate Class With Examples
Java 8 LocalDate Class With Examples
A quick guide to work with the java 8 LocalDate class. Examples to use LocalDate methods.
https://1.bp.blogspot.com/-nUooE742Uow/X7v_3ZYTiWI/AAAAAAAADE0/3QPjKLl5qpooDj8GIxAkqEAPD-a0nAhuwCLcBGAsYHQ/w400-h295/Java%2B8%2BLocalDate%2BClass%2BWith%2BExamples.png
https://1.bp.blogspot.com/-nUooE742Uow/X7v_3ZYTiWI/AAAAAAAADE0/3QPjKLl5qpooDj8GIxAkqEAPD-a0nAhuwCLcBGAsYHQ/s72-w400-c-h295/Java%2B8%2BLocalDate%2BClass%2BWith%2BExamples.png
JavaProgramTo.com
https://www.javaprogramto.com/2020/11/java-localdate.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2020/11/java-localdate.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content