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

前端 未结 5 718
无人共我
无人共我 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:49

    Please check this static utility. You just mentioned a pattern like "ddMMyy" or "HHmm" or what ever you want.. this will work wonderfully.

    public static String getDateTime(XMLGregorianCalendar gDate, String pattern){
    
        return Optional.ofNullable(gDate)
                .map(gdate -> {
                    Calendar calendar = gDate.toGregorianCalendar();
                    SimpleDateFormat formatter = new SimpleDateFormat(pattern);
                    formatter.setTimeZone(calendar.getTimeZone());
                    return formatter.format(calendar.getTime());
                })
                .orElse(null);
    }
    

提交回复
热议问题