Java localdate format: In this article we are going to see the use of Java LocalDate class format() method with suitable examples.
Java LocalDate format() Method with Examples
Java format localdate: This java.time.LocalDate.format(DateTimeFormatter formatter)
method is used to format a specific date by using the specified formatter to create a LocalDate Time. It returns the formatted date string.
Syntax:
public String format(DateTimeFormatter formatter)
Where,
formatter
refers to the parameter object specifying the exact format to be used and this parameter can not be null.
Let’s see the program to understand it more clearly.
Approach:
- Create an objects of LocalDate class which will hold the parsed date.
- Create an object of DateTimeFormatter class and declare the specific date form by passing a pattern as parameter in DateTimeFormatter class.
- Then print the final DateTimeFormatter as result.
Program:
import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class Main { public static void main(String[] args) { //Create an object of LocalDate class and assign a date to it //here it parses the local date LocalDate date = LocalDate.parse("2022-05-10"); System.out.println("Specific date: "+date); //Create an object of DateTimeFormatter and pass a specific format DateTimeFormatter formatDate = DateTimeFormatter.ofPattern("dd/MM/YYYY"); //print the result System.out.println("Formatted date: "+formatDate.format(date)); } }
Output: Specific date: 2022-05-10 Formatted date: 10/05/2022
Note: When you provide the parameter in incorrect format then it gives exception. Let’s illustrate an exception with example.
Approach:
- Create an objects of LocalDate class which will hold the parsed dates.
- Here we pass an invalid date for testing.
- Create an object of DateTimeFormatter class and declare the specific date form by passing a pattern as parameter in DateTimeFormatter class.
- Put all those code inside the try block and in catch block print the exception.
- Then print the final result.
Program:
import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class Main { public static void main(String[] args) { try { //Create an object of LocalDate class and assign a date to it //here it parses the local date LocalDate date = LocalDate.parse("2022-05-40"); System.out.println("Specific date: "+date); //Create an object of DateTimeFormatter and pass a specific format DateTimeFormatter formatDate = DateTimeFormatter.ofPattern("dd/MM/YYYY"); //print the result System.out.println("Formated date: "+formatDate.format(date)); } catch(Exception e) { //print the exception System.out.print(e); } } }
Output: java.time.format.DateTimeParseException: Text '2022-05-40' could not be parsed: Invalid value for DayOfMonth (valid values 1 - 28/31): 40
Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.