I'm using below code to convert java util date to XML gregorian calendar date, but the conversion is somehow adding the time zone to the date.
GregorianCalendar gregorianCalendar = new GregorianCalendar();
gregorianCalendar.setTimeInMillis(dte.getTime());
XMLGregorianCalendar xmlGrogerianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gregorianCalendar);
return xmlGrogerianCalendar;
I don't want the time zone to be the part of the date. Can any one point out how can i achieve this?
util. Date objects do not contain any timezone information by themselves - you cannot set the timezone on a Date object. The only thing that a Date object contains is a number of milliseconds since the "epoch" - 1 January 1970, 00:00:00 UTC.
Actually, Date in Java is always in GMT and it represents a number of milliseconds since 01-01-1970 00:00 and when we print Date, it calls the toString method and displays date-time information in the local timezone.
The setTimeZone() method in DateFormat class is used to set the time-zone of the calendar of this DateFormat. Parameters: The method takes one parameter time_zone of TimeZone type and refers to the new time zone. Return Value: The method does not return any value.
The java. util. Date class represents a particular moment in time, with millisecond precision since the 1st of January 1970 00:00:00 GMT (the epoch time). The class is used to keep coordinated universal time (UTC).
You can change the timezone so it is undefined. Undefined fields (within reason) won't be included in the output.
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gregorianCalendar);
xmlGregorianCalendar.setTimezone( DatatypeConstants.FIELD_UNDEFINED );
You could try setting set the fields individually
GregorianCalendar gregorianCalendar = new GregorianCalendar();
gregorianCalendar.setTimeInMillis(System.currentTimeMillis());
try {
XMLGregorianCalendar xmlGrogerianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar();
xmlGrogerianCalendar.setYear(gregorianCalendar.get(GregorianCalendar.YEAR));
xmlGrogerianCalendar.setMonth(gregorianCalendar.get(GregorianCalendar.MONTH));
xmlGrogerianCalendar.setDay(gregorianCalendar.get(GregorianCalendar.DAY_OF_MONTH));
xmlGrogerianCalendar.setHour(gregorianCalendar.get(GregorianCalendar.HOUR_OF_DAY));
xmlGrogerianCalendar.setMinute(gregorianCalendar.get(GregorianCalendar.MINUTE));
xmlGrogerianCalendar.setSecond(gregorianCalendar.get(GregorianCalendar.SECOND));
System.out.println(xmlGrogerianCalendar.toXMLFormat() );
} catch (DatatypeConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With