How can I display Java date as '12/04/2019'

Java 8Object Oriented ProgrammingProgramming

To format and display date like this i.e. Day/Month/Year, you need to set the pattern:

dd/MM/yyyy

At first, set a LocalDate:

LocalDate localDate = LocalDate.now();

Now format and display date as '12/04/2019':

localDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"))

Example

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Demo {
   public static void main(String[] args) {
      LocalDate date = LocalDate.now();
      System.out.println("Date = "+date);
      DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
      System.out.println("Formatted Date = "+date.format(formatter));
   }
}

Output

Date = 2019-04-12
Formatted Date = 12/04/2019
raja
Published on 25-Apr-2019 15:54:21
Advertisements