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

Java 8Object Oriented ProgrammingProgramming

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

Advertisements