Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Java.util date to XML date without time zone

Tags:

java

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?

like image 907
user964819 Avatar asked Feb 24 '15 11:02

user964819


People also ask

Does Java Util date have timezone?

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.

What timezone does Java date use?

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.

How do I change timezone in date format?

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.

What is Java Util date?

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).


2 Answers

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 );
like image 53
Prisoner Avatar answered Oct 04 '22 04:10

Prisoner


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();
}
like image 36
crowne Avatar answered Oct 04 '22 04:10

crowne