Removing "Z" part from XMLGregorianCalender
Asked Answered
F

6

8

When I do like below,

GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
calendar.setTime(startTime);  // startTime Date
DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);

I get Output like 2015-04-15T11:04:30.000Z.

I want it to be like 2015-04-15T11:04:30.000.

Is there a way to achieve this?

Feune answered 18/5, 2015 at 8:12 Comment(0)
G
2

Do it as follow

DatatypeFactory df; 
try { 
    df = DatatypeFactory.newInstance(); 
    return df.newXMLGregorianCalendar(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")); 
} catch (DatatypeConfigurationException e) { 
    // throw new SomeRuntimeException(e); 
}

Or Extend new class from XMLGregorianCalendar, override toXMLFormat and then delegate ALL the other methods to the contained instance.

class CustomXMLGregorianCalendar extends XMLGregorianCalendar
{
    XMLGregorianCalendar calendar;

    CustomXMLGregorianCalendar(XMLGregorianCalendar calendar){
        this.calendar = calendar;
    }

    public String toXMLFormat() {
        String text = calendar.toXMLFormat();
        int pos = text.indexOf('Z');

        return pos < 0 ? text : text.substring(0,pos);
    }

    public void setTimezone(int offset){ calendar.setTimezone( offset ); }
    // ...
}
Granddaddy answered 18/5, 2015 at 8:19 Comment(1)
Outdated answer. Check Ignas Vaitekunas answer belong to actual answer (Worked for me).Henhouse
F
16

Accepted answer or my Java seems to be outdated because I received this error: The method newXMLGregorianCalendar(String) in the type DatatypeFactory is not applicable for the arguments (SimpleDateFormat)

and I didn't want to extend, so I solved it by removing timezone:

    xmlCalendar.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
Flume answered 15/2, 2016 at 11:1 Comment(1)
DatatypeConstants is by default present java or this constant class added manually. If yes then what is value of FIELD_UNDEFINED.Clarita
L
6

If you want to remove only "Z" from XMLGregorianCalendar object just call this method.

xmlDate.setTimezone( DatatypeConstants.FIELD_UNDEFINED )

Lymph answered 30/12, 2016 at 15:58 Comment(1)
DatatypeConstants is by default present java or this constant class added manually. If yes then what is value of FIELD_UNDEFINED.Clarita
G
2

Do it as follow

DatatypeFactory df; 
try { 
    df = DatatypeFactory.newInstance(); 
    return df.newXMLGregorianCalendar(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")); 
} catch (DatatypeConfigurationException e) { 
    // throw new SomeRuntimeException(e); 
}

Or Extend new class from XMLGregorianCalendar, override toXMLFormat and then delegate ALL the other methods to the contained instance.

class CustomXMLGregorianCalendar extends XMLGregorianCalendar
{
    XMLGregorianCalendar calendar;

    CustomXMLGregorianCalendar(XMLGregorianCalendar calendar){
        this.calendar = calendar;
    }

    public String toXMLFormat() {
        String text = calendar.toXMLFormat();
        int pos = text.indexOf('Z');

        return pos < 0 ? text : text.substring(0,pos);
    }

    public void setTimezone(int offset){ calendar.setTimezone( offset ); }
    // ...
}
Granddaddy answered 18/5, 2015 at 8:19 Comment(1)
Outdated answer. Check Ignas Vaitekunas answer belong to actual answer (Worked for me).Henhouse
A
1

This is because your Locale Timezone, to achieve what you need transform the date using SimpleDateFormat:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

UPDATE when you comment:

I tried formatting it to a String then again parsing it to a Date. Then setting time in Calender object. Does not work. Still i get output as "2015-04-15T11:04:30.000Z"

You must understand that Calendar or Date objects are stored in it's own format, another thing is how you print them, so in this case, to see 2015-04-15T11:04:30.000Z in the Calendar representation does not matters, what you need is to have the correct date 2015-04-15 at 11:04:30 show this in the desired format is just to make user-friendly your output.

The output you get is from Calendar.toString() and the method doc says:

Return a string representation of this calendar. This method is intended to be used only for debugging purposes, and the format of the returned string may vary between implementations. The returned string may be empty but may not be null.

So in order to store the date and the time, your Calendar object is correct. In order to print it, you have to transform it into a String.

Anarthrous answered 18/5, 2015 at 8:14 Comment(2)
I tried formatting it to a String then again parsing it to a Date. Then setting time in Calender object. Does not work. Still i get output as "2015-04-15T11:04:30.000Z".Feune
Calendar's ´toString()` method has is own implementation!!! you need to override this if you want to change it's output, or simply print a string formatted, you dont need to care about how it is stored....Anarthrous
C
1

I had a similar issue. I'm putting this here for anyone that comes along after, but solved my issue similar to how Ignas Vaitekunas eventually did it.

I used

XMLGregorianCalendar xgc = DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar());
xgc.setYear(year);
xgc.setMonth(month);
xgc.setDay(day);
xgc.setHour(hour);
xgc.setMinute(min);
xgc.setSecond(second);
xgc.setTimezone(timezone);

Where year, month, day, hour, min, second, timezone are all initially set to DatatypeConstants.FIELD_UNDEFINED.

I pull values from a formatted text field that's displayed to the user as '    -  -  T  :  :  Z'. The user fills in what information they have, I split and parse the string and if I have a value for the timezone (or second, or minute, or hour etc) I set it, if not it gets fed to xgc as and undefined field. Then the XML Gregorian Calendar will print out only what values aren't undefined.

Conation answered 5/4, 2018 at 13:3 Comment(0)
Z
-1

I had that situation and it was solved, using this line:

XMLGregorianCalendar dateNew = DatatypeFactory.newInstance().newXMLGregorianCalendar("2017-09-06T21:08:14");
Zippy answered 7/9, 2017 at 3:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.