문제

I have a XMLGregorianCalendar that I would like to convert to a Java Date object, but when I try to covert this:

2013-11-19T00:00:00-00:00

I always get a date with the value a day behind.

Mon Nov 18 17:00:00 MST 2013

I just want a date object containing 11/19/2013.

도움이 되었습니까?

해결책 2

I just did this,

Date startDate = new Date(request.getStartTime().getYear(), request.getStartTime().getMonth(), request.getStartTime().getDay(), 0, 0, 0);

다른 팁

As commented above, the result you're getting is right - it's the same moment in time. Midnight UTC is 5PM MST the day before. Perhaps you should look into why your time is in "-00:00" instead of "-07:00" (MST)... but in the meanwhile, I suppose you could try this:

public static void main(String... args) throws DatatypeConfigurationException {
    XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendar("2013-11-19T00:00:00-00:00");

    Calendar c = xcal.toGregorianCalendar();
    c.setTimeZone(TimeZone.getDefault());
    Date d = c.getTime();
    System.out.println(d);
}

prints out Tue Nov 19 00:00:00 EST 2013, and will work for other times of day, not just midnight.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top