Java Program to format date as Apr 19, 2019, 1:27 PM


To format and display datetime, you need to use DateTimeFormatter. The format style is MEDIUM and SHORT:

DateTimeFormatter formatter = DateTimeFormatter .ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT);

Display the formatted date:

formatter.format(LocalDateTime.now()

Example

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class Demo {
   public static void main(String[] args) {
      DateTimeFormatter formatter = DateTimeFormatter .ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT);
      System.out.println("Formatted Date = "+formatter.format(LocalDateTime.now()));
   }
}

Output

Formatted Date = Apr 19, 2019, 1:27 PM

Updated on: 30-Jul-2019

135 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements