Specify the date format in XMLGregorianCalendar

前端 未结 5 685
离开以前
离开以前 2020-12-05 13:58

I want to use a Date in XMLGregorianCalendar format for sending to a web service. The web service expects information in yyyy-dd-mm format. I use t

5条回答
  •  广开言路
    2020-12-05 14:44

    Much simpler using only SimpleDateFormat, without passing all the parameters individual:

        String FORMATER = "yyyy-MM-dd'T'HH:mm:ss'Z'";
    
        DateFormat format = new SimpleDateFormat(FORMATER);
    
        Date date = new Date();
        XMLGregorianCalendar gDateFormatted =
            DatatypeFactory.newInstance().newXMLGregorianCalendar(format.format(date));
    

    Full example here.

    Note: This is working only to remove the last 2 fields: milliseconds and timezone or to remove the entire time component using formatter yyyy-MM-dd.

提交回复
热议问题