How to Format Date and LocalDate in Java?

To format Date and LocalDate in specified formats. Refer the below code snippet:

Code

import java.text.SimpleDateFormat;
import java.util.Date;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class DemoClass {
    public static void main (String[] args){
        SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
        Date date = new Date();
        System.out.println(formatter.format(date));
        LocalDate now = LocalDate.now();
        System.out.pritln(now.format(DateTimeFormatter.ofPattern("MM/dd/yyyy")));
    }
}

Output

Refer given link for SimpleDateFormat pattern formats:

https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Refer given link for DateTimeFormatter pattern formats:

https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

Thank you All!!! Hope you find this useful.


Up ↑

%d bloggers like this: