DateUtil returning wrong date in UTC format | Community
Skip to main content
vjleo94
Level 3
April 4, 2025
Solved

DateUtil returning wrong date in UTC format

  • April 4, 2025
  • 1 reply
  • 613 views

Hi,

I am using "com.day.cq.commons.date.DateUtil" and printing the following in my logs

logger.debug("Raw Calendar Time: " + cal.getTime());
logger.debug("Calendar TimeZone: " + cal.getTimeZone().getID());
logger.debug("ISO: " + DateUtil.getISO8601Date(cal));

And the results are as follows :

Raw Calendar Time: Fri Apr 04 15:46:22 CEST 2025
Calendar TimeZone: Europe/Paris
ISO: 2025-04-04T15:46:22.213Z

However, Here my time zone of the JVM is Europe. However the ISO time printed is also in CEST with a Z at the end. Which should ideally be in UTC.

Please explain.

Best regards,
Vijaya Kumar A

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by konstantyn_diachenko

Hi @vjleo94 ,

 

The issue here is that DateUtil.getISO8601Date(Calendar cal) from com.day.cq.commons.date.DateUtil does not convert the time to UTC before formatting it in ISO 8601 format. Instead, it simply appends the "Z" (which represents UTC) to the timestamp, even if the Calendar object is in a different time zone.

 

  1. Your system time zone is Europe/Paris (CEST, UTC+2 in daylight saving time).
    1. cal.getTime() prints: Fri Apr 04 15:46:22 CEST 2025
    2. This means your calendar object is set to 15:46:22 in CEST.
  2. DateUtil.getISO8601Date(cal) produces:2025-04-04T15:46:22.213Z
    1. The problem: This timestamp includes "Z", which should mean UTC, but the actual time inside the string is still CEST (Europe/Paris time).
    2. Expected output: If it were truly in UTC, the correct value should be 2025-04-04T13:46:22.213Z (subtracting 2 hours from CEST).

Why Is This Happening?

DateUtil.getISO8601Date(Calendar cal) internally calls cal.getTime() and formats the date.

It does not explicitly convert the time to UTC.

Instead, it just appends "Z" to the formatted date, creating a misleading ISO 8601 timestamp.

 

How to Fix It?

  1. Instead of using DateUtil.getISO8601Date(Calendar cal), convert the Calendar instance to UTC before formatting it.
  2. Corrected Approach Using Java 8+
  3. Use ZonedDateTime and DateTimeFormatter:
import java.time.*; import java.time.format.DateTimeFormatter; import java.util.TimeZone; import java.util.Calendar; public class ISODateFix { public static void main(String[] args) { // Create a Calendar instance in Europe/Paris time zone Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Paris")); // Convert Calendar to Instant Instant instant = cal.toInstant(); // Convert to UTC ZonedDateTime ZonedDateTime utcTime = instant.atZone(ZoneId.of("UTC")); // Format correctly in ISO 8601 format String isoCorrected = utcTime.format(DateTimeFormatter.ISO_INSTANT); System.out.println("Corrected ISO Date: " + isoCorrected); } }

 

Conclusion

The issue is that DateUtil.getISO8601Date(cal) does not convert to UTC but still appends "Z", making the output incorrect. You should manually convert the time to UTC before formatting it.

 

Best regards,

Kostiantyn Diachenko.

1 reply

konstantyn_diachenko
Community Advisor
konstantyn_diachenkoCommunity AdvisorAccepted solution
Community Advisor
April 4, 2025

Hi @vjleo94 ,

 

The issue here is that DateUtil.getISO8601Date(Calendar cal) from com.day.cq.commons.date.DateUtil does not convert the time to UTC before formatting it in ISO 8601 format. Instead, it simply appends the "Z" (which represents UTC) to the timestamp, even if the Calendar object is in a different time zone.

 

  1. Your system time zone is Europe/Paris (CEST, UTC+2 in daylight saving time).
    1. cal.getTime() prints: Fri Apr 04 15:46:22 CEST 2025
    2. This means your calendar object is set to 15:46:22 in CEST.
  2. DateUtil.getISO8601Date(cal) produces:2025-04-04T15:46:22.213Z
    1. The problem: This timestamp includes "Z", which should mean UTC, but the actual time inside the string is still CEST (Europe/Paris time).
    2. Expected output: If it were truly in UTC, the correct value should be 2025-04-04T13:46:22.213Z (subtracting 2 hours from CEST).

Why Is This Happening?

DateUtil.getISO8601Date(Calendar cal) internally calls cal.getTime() and formats the date.

It does not explicitly convert the time to UTC.

Instead, it just appends "Z" to the formatted date, creating a misleading ISO 8601 timestamp.

 

How to Fix It?

  1. Instead of using DateUtil.getISO8601Date(Calendar cal), convert the Calendar instance to UTC before formatting it.
  2. Corrected Approach Using Java 8+
  3. Use ZonedDateTime and DateTimeFormatter:
import java.time.*; import java.time.format.DateTimeFormatter; import java.util.TimeZone; import java.util.Calendar; public class ISODateFix { public static void main(String[] args) { // Create a Calendar instance in Europe/Paris time zone Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Paris")); // Convert Calendar to Instant Instant instant = cal.toInstant(); // Convert to UTC ZonedDateTime ZonedDateTime utcTime = instant.atZone(ZoneId.of("UTC")); // Format correctly in ISO 8601 format String isoCorrected = utcTime.format(DateTimeFormatter.ISO_INSTANT); System.out.println("Corrected ISO Date: " + isoCorrected); } }

 

Conclusion

The issue is that DateUtil.getISO8601Date(cal) does not convert to UTC but still appends "Z", making the output incorrect. You should manually convert the time to UTC before formatting it.

 

Best regards,

Kostiantyn Diachenko.

Kostiantyn Diachenko, Community Advisor, Certified Senior AEM Developer, creator of free AEM VLT Tool, maintainer of AEM Tools plugin.
vjleo94
vjleo94Author
Level 3
April 7, 2025

Hi @konstantyn_diachenko ,

Thank you!