BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java LocalDate

By Chaitanya Singh | Filed Under: java

It represents the date in year-month-day format such as 2022-12-05. This class represents the date without a timezone.

 java.time.LocalDate class:

public final class LocalDate
extends Object
implements Temporal, TemporalAdjuster, ChronoLocalDate, Serializable

Java LocalDate class – Method Summary

MethodDescription
LocalDateTime atTime(int hour, int minute)This returns a LocalDateTime formed from this date at the specified hour and minute. The seconds and nanosecond fields will be set to zero.
In order to represent the time in seconds and nanoseconds, use the different variant of this method. This method has total five variants including this one to represent the time part in LocalDateTime in various forms.
boolean equals(Object obj)This method returns true or false. This method checks whether this date is equal to another date.
boolean isLeapYear()This method checks if the year in given date is leap year.
int compareTo(ChronoLocalDate other)It compares the date to another date. Returns integer value, negative if this date is less than other date and positive value if this date is greater than other date.
static LocalDate now()It returns current date specified in the system in default time-zone.
int get(TemporalField field)It returns the value of the specified field from the date as an int value.
long get(TemporalField field)It returns the value of the specified field from the date as long value.
int getDayOfMonth()This method is used to get day of month from the date.
DayOfWeek getDayOfWeek()Returns day of the week for this date. The returned value is in enum which represents the day of the week, for example: 0 represents Sunday, 1 represents Monday, 6 represents Saturday and so on.
int getYear()It returns the year from the date.
int getMonthValue()It returns month of the year, the returned value is integer and it ranges from 1 to 12.
Month getMonth()It returns month of the year in enum.
String format(DateTimeFormatter formatter)This method formats this date using the specified formatter.
int lengthOfMonth()Returns the length of the month represented by this date.
int lengthOfYear()Returns the length of the year represented by this date.
boolean isAfter(ChronoLocalDate other)It checks if this date is after the specified date.
boolean isBefore(ChronoLocalDate other)It checks if this date is before the specified date.
LocalDate minusDays(long daysToSubtract)It returns a copy of this LocalDate after subtracting the specified number of days from it.
LocalDate minusMonths(long monthsToSubtract)It returns a copy of this LocalDate after subtracting the specified number of months from it.
LocalDate minusWeeks(long weeksToSubtract)It returns a copy of this LocalDate after subtracting the specified number of weeks from it.
LocalDate plusDays(long daysToSubtract)It returns a copy of this LocalDate after adding the specified number of days to it.
LocalDate plusMonths(long monthsToSubtract)It returns a copy of this LocalDate after adding the specified number of months to it.
LocalDate plusWeeks(long weeksToSubtract)It returns a copy of this LocalDate after adding the specified number of weeks to it.

Java LocalDate Examples

Example 1: Checking leap year using isLeapYear() method

In the following example, we are using the isLeapYear() method of LocalDate class to find out if the year in a given date is a leap year.

import java.time.LocalDate;
public class JavaExample {
    public static void main(String[] args) {
        LocalDate mydate1 = LocalDate.of(2010, 12, 25);
        System.out.println("Is 2010 leap year? : "+mydate1.isLeapYear());
        LocalDate mydate2 = LocalDate.of(2012, 5, 13);
        System.out.println("Is 2012 leap year? : "+mydate2.isLeapYear());
    }
}  

Output:

Is 2010 leap year? : false
Is 2012 leap year? : true

Example 2: Attaching time to the date

In the following example we are using now() method to get the current date and then we are attaching a specified time to the current date.

import java.time.*;
public class JavaExample {
    public static void main(String[] args) {
        //current date
        LocalDate date = LocalDate.now();

        //Attaching time to the current date
        LocalDateTime datetime = date.atTime(7,32, 59);

        System.out.println("Date with time: "+datetime);
    }
}  

Output:

Date with time: 2022-06-11T07:32:59

Example 3: Finding Yesterday’s date and tomorrow’s date

In this example we are using minusDays() method to get the yesterday’s date and plusDays() method to get the tomorrow’s date.

import java.time.LocalDate;
public class JavaExample {
    public static void main(String[] args) {
        //current date
        LocalDate date = LocalDate.now();

        //yesterday's date  = current date minus one day
        LocalDate lastDay = date.minusDays(1);

        //tomorrow's date = current date plus one day
        LocalDate nextDay = date.plusDays(1);

        System.out.println("Current date: "+date);
        System.out.println("Yesterday's date: "+lastDay);
        System.out.println("Tomorrow's date: "+nextDay);
    }
}

Output:

Current date: 2022-06-11
Yesterday's date: 2022-06-10
Tomorrow's date: 2022-06-12

Example 4: isAfter() and isBefore() methods

In the following example we are comparing the current date to the past date and future date using isAfter() and isBefore() methods.

import java.time.LocalDate;
public class JavaExample {
    public static void main(String[] args) {
        //current date
        LocalDate date = LocalDate.now();

        LocalDate pastDate = date.minusWeeks(3);
        LocalDate futDate = date.plusMonths(6);

        System.out.println(date.isAfter(pastDate));
        System.out.println(date.isBefore(futDate));
    }
}

Output:

true
true
❮ Java TutorialJava Examples ❯

Leave a Reply Cancel reply

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

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap