Detailed Guide on LocalDate in Java

Introduction

Simply put LocalDate in Java is a date without any time or timezone information.

It purely a date which contains year, month and date such 2020-02-02.

LocalDate is immutable object.

Let us see how we can create,parse, modify and retrieve information from the LocalDate with various examples.

At the end we will see how to convert from LocalDate to LocalDateTime.

Creating LocalDate

LocalDate can be created using LocalDate.now() as shown in below example.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import java.time.LocalDate;

public class Examples {

    public static void main(String[] args) {

        LocalDate localDate = LocalDate.now();
        System.out.println(localDate);  // 2024-03-24
    }
}

Below examples in code demonstrates different ways of creating LocalDate.

  1. We can get LocalDate for a given zone id by specifying ZoneId as argument to LocalDate.now(zoneid).
  2. Custom date can be created using LocalDate.of(int year,int month,int dayofmonth).
  3. Obtains instance of Localdate from the epoch day count.
  4. Get the instance of Localdate by specifying year and day of the year.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.time.LocalDate;
import java.time.ZoneId;

public class Examples {

    public static void main(String[] args) {

        LocalDate localDateIndia = LocalDate.now();
        System.out.println(localDateIndia); // 2024-03-24

        //1
        LocalDate localDateLondon = LocalDate.now(ZoneId.of("Europe/London"));
        System.out.println(localDateLondon); // 2024-03-24
        
        //2
        LocalDate customLocalDate = LocalDate.of(2021,06,04);
        System.out.println(customLocalDate);  // 2021-06-04
        
        //3
        LocalDate epochLocalDate = LocalDate.ofEpochDay(2000);
        System.out.println(epochLocalDate); // 1975-06-24

        //4
        LocalDate  ofYearDayLocalDate =LocalDate.ofYearDay(2022,180);
        System.out.println(ofYearDayLocalDate); // 2022-06-29


    }
}

Parsing LocalDate

We can create LocalDate instance using text string such 2022-02-02.

The text string must be valid date and is parsed using DateTimeFormatter.ISO_LOCAL_DATE.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import java.time.LocalDate;

public class Examples {

    public static void main(String[] args) {

        LocalDate localDate = LocalDate.parse("2019-06-06");
        System.out.println(localDate); // 2019-06-06

    }
}

You can also create LocalDate from a text string by using specific DateTimeFormatter or your own custom DateTimeFormatter.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Examples {

    public static void main(String[] args) {

        LocalDate localDate = LocalDate.parse("2019-06-06");
        System.out.println(localDate); // 2019-06-06

        //Using DateTimeFormatter
        LocalDate customLocalDate = LocalDate
                .parse("2018-06-06T10:15:30", DateTimeFormatter.ISO_LOCAL_DATE_TIME);
        System.out.println(customLocalDate); // 2018-06-06

    }
}

Modifying LocalDate

Once we have LocalDate we can modify it using various methods.

As shown in below examples

  1. We can add or subtract days,months,years and weeks using various methods available.
  2. We can have generic method like plus and minus which takes unit to be added or subtracted as first argument and second contains ChronoUnit like days,weeks,month etc.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class Examples {

    public static void main(String[] args) {

        LocalDate localDate = LocalDate.parse("2019-06-06");
        System.out.println(localDate); // 2019-06-06

        System.out.println("-------------------Addition------------------------------");

        // Addition
        System.out.println(localDate.plusDays(10)); // 2019-06-16
        System.out.println(localDate.plusMonths(2)); // 2019-08-06
        System.out.println(localDate.plusYears(2)); // 2021-06-06
        System.out.println(localDate.plusWeeks(5)); // 2019-07-11
        System.out.println(localDate.plus(5, ChronoUnit.DAYS)); //2019-06-11


        System.out.println("-------------------Subtraction------------------------------");

        // Subtraction
        System.out.println(localDate.minusDays(10)); // 2019-05-27
        System.out.println(localDate.minusMonths(2)); // 2019-04-06
        System.out.println(localDate.minusYears(2)); // 2017-06-06
        System.out.println(localDate.minusWeeks(5)); // 2019-05-02
        System.out.println(localDate.minus(5, ChronoUnit.DAYS)); //2019-06-01

    }
}

Retrieving information from Localdate

We can get various information from a given LocalDate like month,day of the week, day of the year, year, month value, no of the day in a month etc as shown in below examples.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import java.time.LocalDate;

public class Examples {

    public static void main(String[] args) {

        LocalDate localDate = LocalDate.parse("2022-06-06");
        System.out.println(localDate); // 2022-06-06

        System.out.println(localDate.getMonth());  // JUNE
        System.out.println(localDate.getDayOfWeek()); // MONDAY
        System.out.println(localDate.getDayOfYear()); // 157
        System.out.println(localDate.getYear()); // 2022
        System.out.println(localDate.getMonthValue()); // 6
        System.out.println(localDate.lengthOfMonth());  // 30

    }
}

LocalDate to LocalDateTime

We can easily convert LocalDate to LocalDateTime by specifying the units of time like minutes, hour,seconds and nanosecond.

We can use atTime on the given instance of LocalDate which takes arguments like minutes, hour,seconds and nanosecond based on our requirement.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.time.LocalDate;
import java.time.LocalDateTime;

public class Examples {

    public static void main(String[] args) {

        LocalDate localDate = LocalDate.parse("2022-06-06");
        System.out.println(localDate); // 2022-06-06

        LocalDateTime hourminsecondsnanos = localDate
                .atTime(12, 03, 06, 03);
        System.out.println(hourminsecondsnanos); // 2022-06-06T12:03:06.000000003

        LocalDateTime hoursandminutes = localDate.atTime(05, 03);
        System.out.println(hoursandminutes); // 2022-06-06T05:03

        LocalDateTime hourminseconds = localDate.atTime(02, 02, 02);
        System.out.println(hourminseconds); // 2022-06-06T02:02:02


    }
}

Conclusion

In this blog we have learnt how to use LocalDate with various examples.

I hope this detailed guide has helped and clarified your doubts.