Languages
[Edit]
EN

Java 8 - How to Format LocalDate examples

4 points
Created by:
Marcin
15680

1. Overview

In this post we cover basic usage of LocalDate class introduced in Java 8.

2. LocalDate with DateTimeFormatter

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Example1 {

    public static void main(String[] args) {

        LocalDate localDate = LocalDate.now();

        String pattern = "yyyy/MM/dd";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        String format = localDate.format(formatter);

        System.out.println("LocalDate.now: ");
        System.out.println(localDate);
        System.out.println("Formatted: ");
        System.out.println(format);
    }
}

Output:

LocalDate.now:
2019-10-13
Formatted:
2019/10/13

3. LocalDate with DateTimeFormatter - most popular patterns

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Example2 {

    public static void main(String[] args) {

        LocalDate localDate = LocalDate.now();

        System.out.println("# Example 1 ");
        System.out.println(localDate); // default ISO_DATE

        System.out.println("# Example 2 ");
        System.out.println(localDate.format(DateTimeFormatter
                .ofPattern("yyyyMMdd")));

        System.out.println("# Example 3 ");
        System.out.println(localDate.format(DateTimeFormatter
                .ofPattern("yyyy/MM/dd")));

        System.out.println("# Example 4 ");
        System.out.println(localDate.format(DateTimeFormatter
                .ofPattern("yyyy-MM-dd")));

        System.out.println("# Example 5 ");
        System.out.println(localDate.format(DateTimeFormatter.ISO_DATE));

        System.out.println("# Example 6 ");
        System.out.println(localDate.format(DateTimeFormatter.
                ofPattern("E, MMM dd yyyy")));

        System.out.println("# Example 7 ");
        System.out.println(localDate.format(DateTimeFormatter
                .ofPattern("EEEE, MMM dd yyyy")));
    }
}

Output:

# Example 1
2019-10-13
# Example 2
20191013
# Example 3
2019/10/13
# Example 4
2019-10-13
# Example 5
2019-10-13
# Example 6
Sun, Oct 13 2019
# Example 7
Sunday, Oct 13 2019

4. String to LocalDate

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Example3 {

    public static void main(String[] args) {

        String date = "2019/10/13";

        String pattern = "yyyy/MM/dd";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        LocalDate localDate = LocalDate.parse(date, formatter);

        System.out.println("LocalDate: ");
        System.out.println(localDate);
    }
}

Output:

LocalDate:
2019-10-13

5. String to LocalDate with build-in formatter

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Example4 {

    public static void main(String[] args) {

        String date = "2019-10-13";

        DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE;
        LocalDate localDate = LocalDate.parse(date, formatter);

        System.out.println("LocalDate: ");
        System.out.println(localDate);
    }
}

Output:

LocalDate:
2019-10-13

References

  1. LocalDate - JavaDoc
  2. DateTimeFormatter - JavaDoc
Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join