Home » Core Java » Java LocalDate Example

About Venkat-Raman Nagarajan

Venkat-Raman Nagarajan
Venkat works for a major IT firm in India and has more than a decade of experience working and managing Java projects for a banking client.

Java LocalDate Example

LocalDate class in Java 8 date time API represents a local date without time zone details. It is located in the java.time.LocalDate object. In this article, we shall look at some of the commonly used methods of LocalDate with some examples.

1. What is LocalDate

LocalDate is an immutable thread-safe date-time object that represents a date, often viewed as year-month-day. Other date fields, such as day-of-year, day-of-week, and week-of-year, can also be accessed. It is a date without a time-zone in the ISO-8601 calendar system.

2. Technologies used

localdate java - java.time.localdate

Technologies used are:

  • Java 8

3. Methods

Let us look at some commonly used methods of LocalDate:

  • LocalDateTime atStartOfDay(): Combines date with the time of midnight to create a LocalDateTime at the start of this date.
  • LocalDateTime atTime(int hour, int minute, int second): Combines the date with a specified hour, minute, second to create a LocalDateTime.
  • int compareTo(ChronoLocalDate other): a comparison primarily based on date, from earliest to latest.
  • boolean equals (Object obj): Checks if this date is equal to another date
  • boolean isAfter(ChronoLocalDate other): Checks if the date is after the specified date. This method only considers the position of two dates on local date-time. It does not take into account the chronology or calendar system
  • boolean isBefore(ChronoLocalDate other): Similar to isAfter considers the position of two dates on local time-line to check if the date is before the specified date.
  • boolean isEqual(ChronoLocalDate other): Checks if this date is equal to the specified date
  • LocalDate minus (TemporalAmount amountToSubtract): Returns a copy of this date with the specified amount subtracted.
  • LocalDate minusDays (long daysToSubtract): Returns a copy of this LocalDate with the specified number of days subtracted. Similarly, we have minusMonths, minusWeeks, and minusYears.
  • LocalDate now(): Obtains current date from system clock in default time-zone.
  • LocalDate now(Clock clock): Obtains current date from the specified clock
  • LocalDate of(int year, int month, int dayOfMonth): Obtains an instance of LocalDate from a year, month and day
  • LocalDate parse(CharSequence text): Obtain an instance of LocalDate from a text string such as 2020-06-01.
  • LocalDate plusDays (long daysToAdd): Returns a copy of this LocalDate with the specified number of days added. Similarly, we have plusMonths, plusWeeks, and plusYears.

4. Java LocalDate Example

Let us look at some examples for the commonly used methods.

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

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

		LocalDate localDate2 = LocalDate.of(2020, 06, 9);
		System.out.println("Current date: "+localDate2);
		System.out.println("localDate1 == localDate2 : "+localDate1.equals(localDate2));

		System.out.println("Current date time: "+localDate1.atStartOfDay());
		LocalDate localDate3 = LocalDate.of(2020, 8, 10);
		System.out.println("compareTo : "+localDate1.isAfter(localDate3));
		
		System.out.println("minus 10 days :"+localDate3.minusDays(10));

	}
}

The above example explains the usage of some of the common methods. The output would be as shown below.

Current date: 2020-06-15
Current date: 2020-06-09
localDate1 == localDate2 : false
Current date time: 2020-06-15T00:00
compareTo : false
minus 10 days :2020-07-31

5. Download source code

That was an example of the Java LocalDate class, which is located in java.time.LocalDate object.

Download
You can download the full source code of this example here: Java LocalDate Example

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments