/**
* XMLGregorianCalendar 转换为Date类型
*/
public static void getServerTime() {
GetServerTimeServiceClient gclient = new GetServerTimeServiceClient();
GetServerTimeServicePortType service = gclient
.getGetServerTimeServiceHttpPort();
XMLGregorianCalendar dateType = service.getServerTime();
int year = dateType.getYear();
int month = dateType.getMonth();
int day = dateType.getDay();
int hour = dateType.getHour();
int minute = dateType.getMinute();
int second = dateType.getSecond();
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day, hour, minute, second);
Date date = calendar.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sdate = sdf.format(date);
System.out.println(sdate);
}
/**
* Date 转换为XMLGregorianCalendar类型
*/
public static void saveTime() {
GetServerTimeServiceClient gclient = new GetServerTimeServiceClient();
GetServerTimeServicePortType service = gclient
.getGetServerTimeServiceHttpPort();
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
javax.xml.datatype.DatatypeFactory dtf = null;
try {
dtf = javax.xml.datatype.DatatypeFactory.newInstance();
} catch (DatatypeConfigurationException e) {
e.printStackTrace();
}
XMLGregorianCalendar dateArgs = dtf.newXMLGregorianCalendar(
calendar.get(calendar.YEAR),
calendar.get(calendar.MONTH)+1,
calendar.get(calendar.DAY_OF_MONTH),
calendar.get(calendar.HOUR),
calendar.get(calendar.MINUTE),
calendar.get(calendar.SECOND),
calendar.get(calendar.MILLISECOND),
calendar.get(calendar.ZONE_OFFSET)/(1000*60));
service.saveDate(dateArgs);
System.out.println(dateArgs);
System.out.println("OK!");
}