Format Date as Apr 19, 2019 1:27 PM in Java



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: 2019-07-30T22:30:25+05:30

177 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements