Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
package org.grails.web.converters.marshaller.json;

import java.text.Format;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;

import org.apache.commons.lang3.time.FastDateFormat;

import grails.converters.JSON;
import org.grails.web.converters.exceptions.ConverterException;
Expand All @@ -37,21 +36,25 @@
*/
public class CalendarMarshaller implements ObjectMarshaller<JSON> {

private final Format formatter;
private static final DateTimeFormatter DEFAULT_FORMATTER =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US)
.withZone(ZoneOffset.UTC);

private final Format legacyFormatter;

/**
* Constructor with a custom formatter.
* @param formatter the formatter
*/
public CalendarMarshaller(Format formatter) {
this.formatter = formatter;
this.legacyFormatter = formatter;
}

/**
* Default constructor.
*/
public CalendarMarshaller() {
this(FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", TimeZone.getTimeZone("GMT"), Locale.US));
this.legacyFormatter = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any known performance problems with the java.time.DateTimeFormatter vs the FastDateTimeFormatter?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

public boolean supports(Object object) {
Expand All @@ -61,7 +64,10 @@ public boolean supports(Object object) {
public void marshalObject(Object object, JSON converter) throws ConverterException {
try {
Calendar calendar = (Calendar) object;
converter.getWriter().value(formatter.format(calendar.getTime()));
String formatted = legacyFormatter != null ?
legacyFormatter.format(calendar.getTime()) :
DEFAULT_FORMATTER.format(calendar.toInstant());
converter.getWriter().value(formatted);
}
catch (JSONException e) {
throw new ConverterException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
package org.grails.web.converters.marshaller.json;

import java.text.Format;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import org.apache.commons.lang3.time.FastDateFormat;

import grails.converters.JSON;
import org.grails.web.converters.exceptions.ConverterException;
Expand All @@ -39,21 +38,25 @@
*/
public class DateMarshaller implements ObjectMarshaller<JSON> {

private final Format formatter;
private static final DateTimeFormatter DEFAULT_FORMATTER =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US)
.withZone(ZoneOffset.UTC);

private final Format legacyFormatter;

/**
* Constructor with a custom formatter.
* @param formatter the formatter
*/
public DateMarshaller(Format formatter) {
this.formatter = formatter;
this.legacyFormatter = formatter;
}

/**
* Default constructor.
*/
public DateMarshaller() {
this(FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", TimeZone.getTimeZone("GMT"), Locale.US));
this.legacyFormatter = null;
}

public boolean supports(Object object) {
Expand All @@ -62,7 +65,11 @@ public boolean supports(Object object) {

public void marshalObject(Object object, JSON converter) throws ConverterException {
try {
converter.getWriter().value(formatter.format(object));
Date date = (Date) object;
String formatted = legacyFormatter != null ?
legacyFormatter.format(date) :
DEFAULT_FORMATTER.format(date.toInstant());
converter.getWriter().value(formatted);
}
catch (JSONException e) {
throw new ConverterException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
package org.grails.web.converters.marshaller.xml;

import java.text.Format;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

import org.apache.commons.lang3.time.FastDateFormat;

import grails.converters.XML;
import org.grails.web.converters.ConverterUtil;
import org.grails.web.converters.exceptions.ConverterException;
Expand All @@ -34,21 +34,25 @@
*/
public class DateMarshaller implements ObjectMarshaller<XML> {

private final Format formatter;
private static final DateTimeFormatter DEFAULT_FORMATTER =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS z")
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The date pattern has been changed from "yyyy-MM-dd HH:mm:ss.S z" (single 'S' for milliseconds) to "yyyy-MM-dd HH:mm:ss.SSS z" (triple 'S'). This changes the output format and is not backward compatible. A date with milliseconds value 5 would previously be formatted as "5" but will now be formatted as "005". This could break existing clients that parse these dates. Consider using the original pattern "yyyy-MM-dd HH:mm:ss.S z" to maintain backward compatibility.

Suggested change
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS z")
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S z")

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to revert or justify and add documentation for this change.

.withZone(ZoneId.systemDefault());

private final Format legacyFormatter;

/**
* Constructor with a custom formatter.
* @param formatter the formatter
*/
public DateMarshaller(Format formatter) {
this.formatter = formatter;
this.legacyFormatter = formatter;
}

/**
* Default constructor.
*/
public DateMarshaller() {
this(FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss.S z"));
this.legacyFormatter = null;
}

public boolean supports(Object object) {
Expand All @@ -57,7 +61,11 @@ public boolean supports(Object object) {

public void marshalObject(Object object, XML xml) throws ConverterException {
try {
xml.chars(formatter.format(object));
Date date = (Date) object;
String formatted = legacyFormatter != null ?
legacyFormatter.format(date) :
DEFAULT_FORMATTER.format(date.toInstant());
xml.chars(formatted);
}
catch (Exception e) {
throw ConverterUtil.resolveConverterException(e);
Expand Down
Loading