Java Program to format date as Apr 14 2019 01:35 PM IST


To format and display datetime, you need to use DateTimeFormatter and use the pattern as:

DateTimeFormatter dtFormat = DateTimeFormatter.ofPattern("MMM dd yyyy hh:mm a z");

Above, the z is the timezone:

MMM dd yyyy hh:mm a z

Now, use the following for zone:

ZonedDateTime dateTime = ZonedDateTime.now();

Example

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Demo {
   public static void main(String[] argv) {
      DateTimeFormatter dtFormat = DateTimeFormatter.ofPattern("MMM dd yyyy hh:mm a z");
      ZonedDateTime dateTime = ZonedDateTime.now();
      String res = dateTime.format(dtFormat);
      System.out.printf("Date = %s %n", res);
   }
}

Output

Date = Apr 14 2019 01:35 PM IST

Updated on: 30-Jul-2019

64 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements