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.
- Your system time zone is Europe/Paris (CEST, UTC+2 in daylight saving time).
- cal.getTime() prints: Fri Apr 04 15:46:22 CEST 2025
- This means your calendar object is set to 15:46:22 in CEST.
- DateUtil.getISO8601Date(cal) produces:2025-04-04T15:46:22.213Z
- The problem: This timestamp includes "Z", which should mean UTC, but the actual time inside the string is still CEST (Europe/Paris time).
- 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?
- Instead of using DateUtil.getISO8601Date(Calendar cal), convert the Calendar instance to UTC before formatting it.
- Corrected Approach Using Java 8+
- 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.