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
Solved! Go to Solution.
Views
Replies
Total Likes
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.
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?
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.
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.
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?
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.
Thank you!
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies