Java Data Type How to - Format date as '23/06/2015'








Question

We would like to know how to format date as '23/06/2015'.

Answer

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
/*  w  w w.j  a v  a2s  .  co  m*/
public class Main {

  public static void main(String[] args) {
    System.out.println(toString(LocalDate.now()));
  }

  public static String toString(LocalDate date) {
      DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
      return date.format(formatter);
  }
}

The code above generates the following result.