Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java LocalDate format() Method with Examples

This method is used to format a date into specified format. It takes an argument of DateTimeFormatter to format the date and returns a date string. For example, we have a date of yyyy/mm/dd format and want to convert it into dd/mm/yyyy format, then this method is helpful. Syntax of method is declared below.

Syntax

public String format?(DateTimeFormatter formatter)

Parameters:

formatter - It specifies the date formate.

Returns:

It returns a formated date string.

Example: Formating A Date

Lets take an example to format a date, here we are using ofPattern() method to specify format pattern and the call format() method on it. It returns a date string.

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

public class DateDemo {
public static void main(String[] args){  
		
		// Take a date
	    LocalDate date = LocalDate.parse("2018-02-03");
		// Displaying date
		System.out.println("Date : "+date);
		// Formatting Date
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/YYYY");
        String localDate = formatter.format(date);
        System.out.println("Date2 : "+localDate);

	}
}


Date : 2018-02-03
Date2 : 03/02/2018

Example: Format Current Date

If we want to format current system date then simply use now() method to get current date and then call format() method. See the below example.

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

public class DateDemo {
public static void main(String[] args){  
		
		// Take a date
	    LocalDate date = LocalDate.now();
		// Displaying date
		System.out.println("Date : "+date);
		// Formatting Date
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/YYYY");
        String localDate = formatter.format(date);
        System.out.println("Date2 : "+localDate);

	}
}


Date : 2020-06-01
Date2 : 01/06/2020

Live Example:

Try with a live example, execute the code instantly with our powerful Online Java Compiler.