JSON dates

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

JSON dates

Matthew Broadhead-2
Using TomEE 7.1.0 Plus

when using jax-rs endpoint the dates in my objects are getting changed
presumably using timezone data.

for example the uk is now in summer time so the date 2019-08-17T00:00:00
becomes 2019-08-16T23:00:00  even when i am not using the time part of
the date.

i am using momentjs and moment(date).utc() is supposed to bring the date
back to utc but that does not work as the json doesn't seem to include
the original date but the recalculated date

i also tried using a date converter like
public class DateConverter implements Converter<Date> {
     @Override
     public String toString(final Date instance) {
         final Calendar cal = GregorianCalendar.getInstance();
         cal.setTime(instance);
         return DatatypeConverter.printDateTime(cal);
     }

     @Override
     public Date fromString(final String text) {
         return DatatypeConverter.parseDateTime(text).getTime();
     }
}
but it doesn't make any difference

is there any documentation on this?
Reply | Threaded
Open this post in threaded view
|

Re: JSON dates

Matthew Broadhead-2
it looks fixed if i change my converter to work like this but it is
annoying to have to annotate every date in the DTOs.  is there a global
setting?

public class DateConverter implements Converter<Date> {

     private static final String iso8601 = "yyyy-MM-dd'T'HH:mm:ss";

     @Override
     public String toString(final Date instance) {
         SimpleDateFormat sdf = new SimpleDateFormat(iso8601);
         String string = sdf.format(instance);
         return string;
     }

     @Override
     public Date fromString(final String text) {
         return DatatypeConverter.parseDateTime(text).getTime();
     }
}


On 21/08/2019 11:17, Matthew Broadhead wrote:

> Using TomEE 7.1.0 Plus
>
> when using jax-rs endpoint the dates in my objects are getting changed
> presumably using timezone data.
>
> for example the uk is now in summer time so the date
> 2019-08-17T00:00:00 becomes 2019-08-16T23:00:00  even when i am not
> using the time part of the date.
>
> i am using momentjs and moment(date).utc() is supposed to bring the
> date back to utc but that does not work as the json doesn't seem to
> include the original date but the recalculated date
>
> i also tried using a date converter like
> public class DateConverter implements Converter<Date> {
>     @Override
>     public String toString(final Date instance) {
>         final Calendar cal = GregorianCalendar.getInstance();
>         cal.setTime(instance);
>         return DatatypeConverter.printDateTime(cal);
>     }
>
>     @Override
>     public Date fromString(final String text) {
>         return DatatypeConverter.parseDateTime(text).getTime();
>     }
> }
> but it doesn't make any difference
>
> is there any documentation on this?