Java – remove the “Z” section from xmlregistry calender

When I like it below,

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

The output I get is like 2015-04-15t11:04:30.000z

I hope it looks like 2015-04-15t11:04:30.000

Is there any way to achieve this goal?

Solution

This is done as follows

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 the new class from xmlregionancalendar, override toxmlformat, and delegate all other methods to the included 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 ); }
    // ...
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>