简体   繁体   中英

Convert String of type “2015-23-07T00:00:00Z” to XMLGregorianCalender of format “07/23/2015T00:00:00Z”

I am calling a Web Service which accepts date as Xmlgregoriancalendar of format "07/23/2015T00:00:00Z" but what I have currently from my database is "2015-23-07T00:00:00Z" .

How to convert this String type of XMLGregorianCalender of type

"MM/dd/yyyy'T'HH:mm:ss.SSS'Z'"

Since that service is hosted by some third party I can't change the schema and need to implement this conversion.

I tried this

Date d = new Date();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy/MM/dd'T'HH:mm:ss.SSS'Z'");                                                       
String formattedDate1 = sdf1.format(d);                                                  
Date date = sdf1.parse(formattedDate1);                                                
GregorianCalendar gregorianCalendar;
XMLGregorianCalendar result = null;
gregorianCalendar = (GregorianCalendar)GregorianCalendar.getInstance();
gregorianCalendar.setTime(date);
result = DatatypeFactory.newInstance().newXMLGregorianCalendar(gregorianCalendar);
java.text.DateFormat outputFormat =new java.text.SimpleDateFormat("MM/dd/yyyy'T'HH:mm:ss'Z'");
java.text.DateFormat outputFormat1 =new java.text.SimpleDateFormat("yyyy-dd-MM'T'HH:mm:ss'Z'");
System.out.println(outputFormat.format(outputFormat1.parse("2015-23-07T00:00:00Z")));//07/23/2015T00:00:00Z

You can using SimpleDateFormat to do this.

Grab the string from database, find and replace "-" with "\\" then parse String to Date. Actually, SimpleDateFormat would accept "MM-dd-yyyy'T'HH:mm:ss.SSS'Z'" type as well as "MM/dd/yyyy'T'HH:mm:ss.SSS'Z'" and you can do some calculation on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM