Skip to content
Advertisement

Java formatting GMT Date

I’m not finding a way to do edit GMT Date

I receive “params.date” one string in this format “yyyyMMdd”, so this is my flow:

JavaScript

output:

JavaScript

And I need it:

JavaScript

Can someone help me?

Advertisement

Answer

A few important points:

  1. Thu Nov 17 21:00:00 GMT-03:00 2022 is not equal to Thu Nov 17 21:00:00 GMT-00:00 2022. The date-time of Thu Nov 17 21:00:00 GMT-03:00 2022 is equal to the date-time of Fri Nov 18 00:00:00 GMT+00:00 2022 which you can obtain by adding 3 hours to Thu Nov 17 21:00:00 GMT-03:00 2022.
  2. Your time-zone has an offset of -03:00 and therefore the java.util.Date object is being displayed with the default time-zone offset. Note that java.util.Date does not represent a real date-time, rather, it gives you the number of milliseconds from the Epoch and then the Date#toString implementation applies the system’s time-zone to render the string.
  3. 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.

Demo with java.time API:

JavaScript

Output:

JavaScript

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

Advertisement