0

I have an DateAdapter

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 enter "2009-12-31T23:59:59.999 +0000" when transfer String to Date

But when try to parse from Date to String -> 2010-01-01T02:59:59.999 +0300 (hours changed and time zone too)

So, how to disable time transfering?

Roberto
  • 1,288
  • 5
  • 23
  • 47
  • 2
    If using java.util.Date there is no way to "disable time zone transfering". Once it is converted to object Date then the time zone information is lost and therefore you can't know what was the time zone in the original String. But if you want to force the dates to be formatted as UTC, all you need to do is to do setTimeZone() on the dateFormat and specify UTC. – kumesana Jan 16 '19 at 15:13
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `OffsetDateTime` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jan 16 '19 at 15:19
  • Possible duplicate of [DateFormat parse - not return date in UTC](https://stackoverflow.com/questions/47307909/dateformat-parse-not-return-date-in-utc) – Ole V.V. Jan 16 '19 at 15:21
  • Are you sure you get `2009-12-31T23:59:59.999 +0000` with a space before `+0000`? It looks very much like an attempt to give the standard ISO 8601 format, but that format doesn’t have the space. It also looks like an attempt to give “the last moment of the day”, but that’s nonsense. Better to give either only the date without time of day, or give the time at midnight and let the application know that it’s the end of the day *exclusive*. – Ole V.V. Jan 16 '19 at 15:26
  • 1
    FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jan 16 '19 at 21:02

3 Answers3

1

tl;dr

Always use java.time classes, never the terrible legacy classes such as java.util.Date. To interoperate, convert.

I suspect you actually intended standard ISO 8601 format such as 2009-12-31T23:59:59.999Z.

Do not be tripped-up by the anti-feature in Date::toString that dynamically applies a time zone to a value that is actually in UTC.

The java.time classes are thread-safe by design, using immutable objects. So no need for the synchronized calls.

public class DateAdapter extends XmlAdapter< String, Date > {

    @Override
    public String marshal( Date v ) throws Exception {      // `Instant` generates standard ISO 8601 strings by default. No need to specify a formatting pattern. 
        return v.toInstant().toString() ;                   // Convert from legacy `Date` to modern `Instant`. 
    }

    @Override
    public Date unmarshal( String v ) throws Exception {    // `Instant` parses standard ISO 8601 strings by default. No need to specify a formatting pattern. 
        return java.util.Date.from( Instant.parse( v ) ) ;  // Convert from modern `Instant` to legacy `Date`. 
    }
}

ISO 8601

Your input string is close to standard ISO 8601 format. I strongly recommend sticking with the standard wherever possible. Omit the SPACE before the offset-from-UTC.

And, best to include the COLON in the offset between the hours and minutes — while optional in the standard, some libraries expect the character.

String input = "2009-12-31T23:59:59.999+00:00" ;

By the way, a common abbreviation of an offset-from-UTC of zero is the letter Z, pronounced “Zulu”.

String input = "2009-12-31T23:59:59.999Z"  ;

java.time

You are using terrible date-time classes bundled with the earliest versions of Java. Those legacy classes were supplanted entirely years ago by the java.time classes.

Check to see if your libraries have been updated to work with java.time. If not, you can convert back-and-forth by calling new methods added to the old classes.

A java.util.Date object is a moment, a point on the timeline in UTC. Now replaced by the java.time.Instant class.

Instant instant = myJavaUtilDate.toInstant() ;   // Convert by calling new method added to the old class.

To generate a string in the standard format with a Z, just call toString.

String output = instant.toString() ;

Parsing a string in standard format is simple with Instant.parse. No need to specify a formatting pattern.

Instant instant = Instant.parse( "2009-12-31T23:59:59.999Z" ) ;

Convert using another new method added to the old class.

java.util.Date d = java.util.Date.from( instant ) ;

If you cannot change to using standard ISO 8601 text formats, use a DateTimeFormatter object to parse and generate strings in alternate formats. Search Stack Overflow as this has been covered many many times already.

Date::toString lies

Be aware that java.util.Date::toString lies to you. That method dynamically applies the JVM’s current default time zone. The internal value is in UTC, but this method reports otherwise. While well-intentioned, this anti-feature has caused no end of confusion and misery among Java programmers. In contrast, the java.time classes report their values directly without such opinionated injections.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

I think your system sets time zone as System timezone. For output you might use another SimpleDateFormat without timezone i.e

private final SimpleDateFormat noTimeZoneDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");

you can also use this dateformat for input if you don't need timezone at all

Bukas
  • 35
  • 1
  • 8
  • 1
    The first suggestion will still calculate time zone differences and output as local time zone rather than UTC. The second suggestion will produce incorrect data on DST switches. – kumesana Jan 16 '19 at 15:20
  • FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jan 16 '19 at 21:02
0

The result you get is absolutely correct (but obviously not what you want). It is exactly the same moment in time as the one you parsed, only represented differently. There are zillions of different strings how to write down a given moment in time, and one of the parameters is the preferred timezone.

You seem to live in a +0300 timezone, and so SimpleDateFormat prefers to represent any given Date according to this timezone, no matter what the input timezone was.

If you want to retain not only the original moment in time, but also information about the source representation (including the timezone specified), then Date can't model that (unless you want to use the long-outdated, deprecated part of its API), and you have to look for a different representation class.

With Date and SimpleDateFormat, the properties of the SimpleDateFormat decide about the resulting representation, one property being the format string, and another one being the preferred timezone. You set the latter by calling setTimeZone() on the SimpleDateFormat instance. This way, you can decide which (fixed) timezone gets used while formatting, e.g. that you want all Dates represented according to UTC.

Ralf Kleberhoff
  • 6,990
  • 1
  • 13
  • 7