String,Date,XMLGregorianCalendar的转换

时间:2021-09-18 23:09:27

常见标准的写法"yyyy-MM-dd HH:mm:ss",区分大小写,时间是24小时制,24小时制转换成12小时制只需将HH改成hh。

String to Date:

String sdate = "2016-3-14";
SimpleDateFormate sdf = new SimpleDateFormate("yyyy-MM-dd");
Date date = sdf.parse(sdate);

Date to String:

sdate = (new SimpleDateFormate("yyyy-MM-dd")).formate(date);

Date to XMLGregorianCalendar:

        GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
XMLGregorianCalendar gc = null;
try {
gc = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
} catch (Exception e) { e.printStackTrace();
}
     return gc;