If you use dates over a Web Service using Apache Axis you may find a quite tricky issue when dealing with them.
Imagine you want to send over a Web Service call a date time using Calendar class.
Calendar myCalendar = new GregorianCalendar();
and want to pass your calendar variable over the Web Service.
If you log the XML send over Axis
http://rafareyeslopez.blogspot.com/2015/04/logging-axis-xml.html
You will notice that the date time is passed in UTC
Example:
2002-05-30T09:30:10Z
So if the other endpoint does not do anything to check about time zones you get in trouble, specially if time is close to midnight and the correct date has to be used.
After trying several approaches such us specify the Time Zone for the Calendar such as:
TimeZone tz = TimeZone.getTimeZone("Europe/Madrid");
Calendar date = Calendar.GetInstance(tz);
date.setTimeInMillis( millis );
It didn't go well
Best solution I found after struggling setting up the right Time Zone to the Calendar passed to the Web Service (always setting time zone and then the different calendar values) is use this method:
public static Calendar convertToGmt(Calendar cal) {
Date date = cal.getTime();
TimeZone tz = cal.getTimeZone();
// Returns the number of milliseconds since January 1, 1970, 00:00:00
// GMT
long msFromEpochGmt = date.getTime();
// gives you the current offset in ms from GMT at the current date
int offsetFromUTC = tz.getOffset(msFromEpochGmt);
// create a new calendar in GMT timezone, set to this date and add the
// offset
Calendar gmtCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
gmtCal.setTime(date);
gmtCal.add(Calendar.MILLISECOND, offsetFromUTC);
return gmtCal;
}