Java LocalDate class

Java LocalDate class

Java LocalDate class is an immutable class that represents Date with a default format of yyyy-MM-dd. It inherits Object class and implements the ChronoLocalDate interface


Java LocalDate class declaration

Let’s see the declaration of java.time.LocalDate class.

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

Methods of Java LocalDate

Method Description
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.

 

Java LocalDate Example

  1. import java.time.LocalDate;
  2. public class LocalDateExample {
  3.   public static void main(String[] args) {
  4.     LocalDate date = LocalDate.now();
  5.     LocalDate yesterday = date.minusDays(1);
  6.     LocalDate tomorrow = yesterday.plusDays(2);
  7.     System.out.println(“Today date: “+date);
  8.     System.out.println(“Yesterday date: “+yesterday);
  9.     System.out.println(“Tommorow date: “+tomorrow);
  10.   }
  11. }

Output:

Today date: 2017-01-13
Yesterday date: 2017-01-12
Tommorow date: 2017-01-14

Java LocalDate Example: isLeapYear()

  1. import java.time.LocalDate;
  2. public class LocalDateExample {
  3.   public static void main(String[] args) {
  4.     LocalDate date1 = LocalDate.of(2017113);
  5.     System.out.println(date1.isLeapYear());
  6.     LocalDate date2 = LocalDate.of(2016923);
  7.     System.out.println(date2.isLeapYear());
  8.   }
  9. }

Output:

false
true

Java LocalDate Example: atTime()

  1. import java.time.*;
  2. public class LocalDateExample {
  3.   public static void main(String[] args) {
  4.     LocalDate date = LocalDate.of(2017113);
  5.     LocalDateTime datetime = date.atTime(1,50,9);
  6.     System.out.println(datetime);
  7.   }
  8. }

Output:

2017-01-13T01:50:09
Next TopicJava LocalTime
java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Java LocalDate class Java LocalDate class is an immutable class that represents Date with a default format of yyyy-MM-dd. It inherits Object class and implements the ChronoLocalDate interface Java LocalDate class declaration Let’s see the declaration of java.time.LocalDate class. public final class LocalDate extends Object implements Temporal, TemporalAdjuster, ChronoLocalDate, Serializable Methods of Java LocalDate Method Description LocalDateTime atTime(int hour, int minute) It is used […]

Tags:

....
...