What is the best way to convert XMLGregorianCalendar to MM/dd/yyyy hh:mm String?

前端 未结 5 809
无人共我
无人共我 2021-02-04 00:18

What is the best way to convert XMLGregorianCalendar objects to \'MM/dd/yyyy hh:mm\' String?

5条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-04 00:51

    First use XMLGregorianCalendar#toGregorianCalendar() to get a java.util.Calendar instance out of it.

    Calendar calendar = xmlGregorianCalendar.toGregorianCalendar();
    

    From that step on, it's all obvious with a little help of SimpleDateFormat the usual way.

    SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm");
    formatter.setTimeZone(calendar.getTimeZone());
    String dateString = formatter.format(calendar.getTime());
    

    I only wonder if you don't actually want to use HH instead of hh as you aren't formatting the am/pm marker anywhere.

提交回复
热议问题