Java LocalDateTime format to String like "03/19/20 09:31:19"

Description

Java LocalDateTime format to String like "03/19/20 09:31:19"

import java.time.DateTimeException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Main {

   public static void main(String[] args) {

      try {/*w  w w  .j  a v a  2 s.  co m*/
         LocalDateTime now = LocalDateTime.now();
         DateTimeFormatter dateFormatter2 = DateTimeFormatter.ofPattern("MM/dd/YY HH:mm:ss");
         String output2 = now.format(dateFormatter2);
         System.out.println(output2);

      } catch (DateTimeException ex) {
         System.out.println("Cannot be formatted: " + ex);
      }

   }
}



PreviousNext

Related