0

I am trying to print a date in this format:

2023-01-11 09:25:52 UTC

But when I use date format:

yyyy-MM-dd HH:mm:ss Z

I get:

2023-01-11 09:29:25 +0100

While searching by existing question in stack overflow, I found similar questions but not with this exact format with "UTC" at the end. I found one that provided a solution to add the format as yyyy-MM-dd HH:mm:ss 'UTC' but then it would force the a GMT value to wrongly show UTC at the end.

Most of the answers explain how to get UTC value, but not how to print in this format

2023-01-11 09:25:52 UTC

Some solutions were also suggesting to use something else than SimpleDateFormat.

This question was marked as duplicated, but none of the post that were supposed to be duplicated had the info that I wanted.

Ricardo Machado
  • 784
  • 6
  • 22
  • 2
    Switch from the error-prone legacy date-time API to the modern date-time API. [Here](https://ideone.com/gSvgsH) is a solution using the modern API. – Arvind Kumar Avinash Jan 11 '23 at 08:56
  • By *a date* did you mean a `java.util.Date`? I strongly recommend you neither use that class, it’s poorly designed and nothing you should struggle with, nor the notoriously troublesome `SimpleDateFormat` class. Use [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/index.html). For example `ZonedDateTime.now(ZoneId.of("Etc/UTC")).format(DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss zzz", Locale.ROOT))` (just gave `2023-01-11 14:23:11 UTC`). – Ole V.V. Jan 11 '23 at 14:24
  • Did you remember to search before asking? What did or did you not find? Very similar questions have been asked and answered a lot of times before. – Ole V.V. Jan 11 '23 at 15:37
  • If you are getting a `Date` from a legacy API that you cannot upgrade, use `yourOldfashionedUtilDate .toInstant() .atZone(ZoneId.of("Etc/UTC")) .format(DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss zzz", Locale.ROOT))`. Example result is `2023-01-11 09:25:52 UTC`. – Ole V.V. Jan 11 '23 at 15:44

2 Answers2

1

Use a lowercase z instead of Z to get the offset instead of the id. And you have to set the time zone using simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")).

Example:

Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(simpleDateFormat.format(date)); // 2023-01-11 08:41:17 UTC
Alexis
  • 76
  • 1
  • 1
  • 8
  • I tried this same thing but with an Upper case Z and it didn't work. I tried with lower case z as in you example and it worked percet. Thanks. – Ricardo Machado Jan 11 '23 at 08:53
  • While your code gives the desired output, neither the OP nor you nor anyone else should use `Date` and `SimpleDateFormat`. They are troublesome and long outdated. – Ole V.V. Jan 11 '23 at 14:25
0

If you create an instance of simpleDateFormat with yyyy-MM-dd HH:mm:ss Z date format is incorrect change to capital Z to small z,

then it works as expected.

2023-01-11 09:25:52 UTC

Jai Prakash
  • 162
  • 1
  • 10