the Date Thu Sep 27 00:00:00 CEST 2018
becomes
2018-09-26T22:00:00.000Z
when converted in XMLGregorianCalendar
by this method:
public static XMLGregorianCalendar dateToXMLGregorianCalendar(Date date) throws DatatypeConfigurationException{
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
return DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
}
How can I avoid this date change?
Tnx
That is the same date & time.
Your first date is in timezone CEST, which is equivalent to UTC+2 Your second timestamp is rendered in Z timezone, which is equivalent to UTC+0
Thus, both mean the same point in time, but just render differently.
GregorianCalendar has a method to change the calendars timezone, if you want output in CEST date & time then I suggest you change the calendar timezone to that. See GregorianCalendar.setTimeZone for API reference.
It also kind of depends on how you retrieve your date from that calendar. If you just plan to use the
.getTime()method that returns a plain Java Date object, then you can send this through a DateTimeFormatter to output the date in whatever timezone you desire.If you only care about the actual "date" part of the date, and don't want to adapt it to various timezones, have a look at LocalDate instead.