Java Data Type How to - Format date time as '23/06/2010 09:50:49'








Question

We would like to know how to format date time as '23/06/2010 09:50:49'.

Answer

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/*www . j av  a2  s . c  om*/
public class Main {

  public static void main(String[] args) {
    String DATE_FORMAT = "dd/MM/yyyy HH:mm:ss";

    LocalDateTime ldt = LocalDateTime.now();
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern(DATE_FORMAT);

    System.out.println(ldt.format(dtf));
  }

}

The code above generates the following result.