Java Data Type How to - Convert date from '02/06/2015' to '2015-02-06'








Question

We would like to know how to convert date from '02/06/2015' to '2015-02-06'.

Answer

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
/*ww  w .jav a2s .  c om*/
public class Main {

  public static void main(String[] args) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M/d/y");
    LocalDate maxCloseDate = LocalDate.parse("02/06/2015", formatter);
    System.out.println(maxCloseDate);
  }
}

The code above generates the following result.