HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Date Time / Java LocalDate class

Java LocalDate class

java.time.LocalDate class, introduced in Java 8, represents a local date without timezone and time of that day e.g. 2019-03-27. It has the time portion as the moment of the day starts i.e. minutes and seconds are considered zero.

We can use the LocalDate instances where we need to represent a day without specific time of the day – such as birthday, holiday or leaves taken by an employee.

The primary difference between a java.util.Date and java.time.LocalDate is that LocalDate instances are immutable and thread-safe.

1. Class declaration

public final class LocalDate
	extends Object
	implements Temporal, TemporalAdjuster, ChronoLocalDate, Serializable
{
	//class body
}

2. How to create LocalDate

Generally, we will be creating local date instances in two conditions i.e. get current date or create local date for a specified day.

2.1. Get current local date

Use following methods to get the current local date.

LocalDate today = LocalDate.now();	//1 - Recommended

LocalDate today = LocalDate.ofInstant(Instant.now(), ZoneId.systemDefault());	//

2.2. Create specified local date

To create a local date with specific day, month and year – use below methods.

LocalDate dateInstance = LocalDate.of(2019, 3, 27);

LocalDate dateInstance = LocalDate.parse("2019-03-27");

3. How to parse string to LocalDate

The LocalDate class has two overloaded parse() methods to convert string date to local date instance.

parse(CharSequence text)	//1

parse(CharSequence text, DateTimeFormatter formatter)	//2
  • Use first method if the string contains date in ISO_LOCAL_DATE pattern i.e. yyyy-MM-dd. This is default pattern of local dates in Java.
  • For any other date pattern, we need to use second method where we pass the date string as well as a formatter which represents the pattern if that date string.
//1 - default date pattern
String date = "2019-03-23";
LocalDate localDate = LocalDate.parse(date);

//2 - specific date pattern
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d-MMM-yyyy");
String date = "23-Mar-2019";
LocalDate localDate = LocalDate.parse(date, formatter);

4. How to format LocalDate to string

Use LocalDate.format(DateTimeFormatter) method to format a local date to a desired string representation.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d-MMM-yyyy");

LocalDate today = LocalDate.now();

String dateString = today.format(formatter);	//23-Mar-2019

5. How to modify local dates

LocalDate provides below methods which can be used to get to new local date instance relative to available local date object.

  • plusDays()
  • plusWeeks()
  • plusMonths()
  • plusYears()
  • minusDays()
  • minusWeeks()
  • minusMonths()
  • minusYears()
LocalDate today = LocalDate.now();

//Same date 3 years later
LocalDate localDate1 = today.plusYears(3);	

//local date before 3 months
LocalDate localDate2 = today.minusMonths(3);

6. Java LocalDate examples

Let’s get better understanding about LocalDate class using some examples.

  • Parse String to LocalDate in Java
  • Format LocalDate to String in Java
  • Convert LocalDate to java.util.Date in Java
  • Convert Date to LocalDate in Java
  • Convert LocalDate to java.sql.Date in Java
  • Convert LocalDate to ZonedDateTime in Java
  • Convert LocalDate to LocalDateTime in Java

Happy Learning !!

Ref : LocalDate Java Doc

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Java Date Time Tutorial

  • Java – Date Time APIs
  • Java – Date Parsing
  • Java – Date Formatting
  • Java 8 – LocalDate
  • Java 8 – LocalTime
  • Java 8 – LocalDateTime
  • Java 8 – ZonedDateTime
  • Java 8 – Period
  • Java 8 – DateTimeFormatter
  • Java 8 – TemporalAdjusters
  • Java 8 – TemporalQuery
  • Java 8 – DayOfWeek
  • Java – Date
  • Java – Locale

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)