1

If I want my formatted date to look like:

2021-07-21 2:02:08.483 p.m. EDT

Can the SimpleDateFormat class print "EDT" or "EST", without me hard-coding it? What would be the format specifier? So far I have:

yyyy-MM-dd HH:mm:ss.SSS a

CNDyson
  • 1,687
  • 7
  • 28
  • 63
  • 3
    Yes, SimpleDateFormat provides timezone format characters. Look here: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/text/SimpleDateFormat.html. Also consider Java8 [DateTimeFormatter](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/format/DateTimeFormatter.html) – paulsm4 Jul 21 '21 at 14:27
  • Thanks for your quick reply! I should have looked here first. – CNDyson Jul 21 '21 at 14:32
  • I recommend you don’t use `SimpleDateFormat`. That class is notoriously troublesome and long outdated. Instead use `DateTimeFormatter` and `ZonedDateTime`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jul 22 '21 at 18:02

2 Answers2

6

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Solution using java.time, the modern Date-Time API:

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd H:mm:ss a zzz", Locale.CANADA);
        String strDateTime = ZonedDateTime.now(ZoneId.of("America/New_York")).format(dtf);
        System.out.println(strDateTime);
    }
}

Output:

2021-07-21 10:34:12 a.m. EDT

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.

Just for the sake of completeness

Just for the sake of completeness, given below is the solution using SimpleDateFormat.

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class Main {
    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd H:mm:ss a zzz", Locale.CANADA);
        sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
        String strDateTime = sdf.format(new Date());
        System.out.println(strDateTime);
    }
}

Output:

2021-07-21 10:36:52 a.m. EDT

ONLINE DEMO


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Thank you! Unfortunately I'm working with some legacy code that uses java.util.Date throughout, but I'm accepting your answer because you gave me the abbreviation for the time zone. Appreciate it! – CNDyson Jul 21 '21 at 14:37
  • 1
    @CNDyson - No worries. I've added a solution using `SimpleDateFormat` as well. Feel free to comment in case of any doubt/issue. – Arvind Kumar Avinash Jul 21 '21 at 14:39
0

You just need to add a z to your pattern - but next time, refer to the docs.

maio290
  • 6,440
  • 1
  • 21
  • 38
  • Got it. But if I use a "z", how to specify that I want the abbreviation "EDT", and not "Eastern Daylight Time"? – CNDyson Jul 21 '21 at 14:30