There is a rest service that accepts xml and processes it with the jaxb library. For one of the fields an adapter for the date is written:

public class DateAdapter extends XmlAdapter<String, Date> { private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z"); @Override public String marshal(Date v) throws Exception { synchronized (dateFormat) { return dateFormat.format(v); } } @Override public Date unmarshal(String v) throws Exception { synchronized (dateFormat) { return dateFormat.parse(v); } } } 

So, I transfer the date "2009-12-31T23: 59: 59.999 +0000" and the adapter converts from string to date

Then I pull the object out of the database and pass it to the client, that is, Date to String -> 2010-01-01T02: 59: 59.999 +0300 (the clock and time shift have been added) and it turns out that I give it with a time offset.

How to disable this conversion, dependent on the server on which the code is running?

  • one
    Translate the question into Russian - cpp questions
  • I suspect that you have a +3 system time zone? - gooamoko 3:05 pm
  • Why disable it? This is the same moment in time, only recorded differently. - Enikeyschik

1 answer 1

The fact is that you apparently use the util.Date class. The implementation of this class is the storage of a number of milliseconds UTC from some point in time X. But since this class describes a point in time, it focuses on the system time zone.

Therefore, as soon as you parsed the time 2009-12-31T23: 59: 59.999 +0000 at util.Date, this class has a number of milliseconds UTC that correspond to this point in time, but the actual representation of the util.Date class is oriented to the default temporary zone (you probably zone +03) and there is no possibility to manage the zone for this class, hence the formatter SimpleDateFormat does not know what the zone was before the time was saved in the util.Date at the time of the inverse transformation, and focuses on the devolt zone.

Before attempting to build an application architecture with respect to time, I would recommend reading this standard describing possible types of time - Working with Time Zones . In order to determine what type of time you are working with and then take steps to present the date in the system.

In this case, I can suggest you look at the classes that know how to manage your zone and use them in the transformations. For example, for java version 8 and higher - ZonedDateTime, for earlier versions - joda DateTime.