Java LocalDateTime format to String like "March 19 2020"

Description

Java LocalDateTime format to String like "March 19 2020"

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

public class Main {

   public static void main(String[] args) {

      try {/*from w  ww  .ja va 2s. co m*/
         DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("MMMM dd yyyy");

         LocalDateTime now = LocalDateTime.now();
         String output = now.format(dateFormatter);
         System.out.println(output);
         
     } catch (DateTimeException ex) {
         System.out.println("Cannot be formatted: " + ex);
     }

   }
}



PreviousNext

Related