Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to print UTC or GMT using SimpleDateFormat?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading