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

Java 8Object Oriented ProgrammingProgramming

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
raja
Updated on 30-Jul-2019 22:30:25

Advertisements