Home  >  Forum  >  Core Java
Post New Query

How to format LocalDate in Java?



Asked on December 06, 2018
Java 8 LocalDate is giving date format as yyyy-MM-dd. How can I change it to dd-MM-yyyy?




Replied on December 06, 2018
Use LocalDate format() method and pass DateTimeFormatter. Find the code.


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

public class LocalDateDemo {
  public static void main(String[] args) {
LocalDate localDate = LocalDate.parse("2018-02-18");

String formattedDate = localDate.format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));
System.out.println(formattedDate);

formattedDate = localDate.format(DateTimeFormatter.ofPattern("MMM dd, yyyy"));
System.out.println(formattedDate);
  }

Output

18-02-2018
Feb 18, 2018 


Write Answer








©2019 concretepage.com | Privacy Policy | Contact Us