赞助

Removing Timezone from XMLGregorianCalendar

 

1、去掉時間之後的“Z”或者修改時區

 1 package Package0809;
 2 
 3 import javax.xml.datatype.DatatypeConfigurationException;
 4 import javax.xml.datatype.DatatypeConstants;
 5 import javax.xml.datatype.DatatypeFactory;
 6 import java.util.Date;
 7 import java.util.GregorianCalendar;
 8 /**
 9  * @author Allen.Lv
10  * @version 1.0
11  * date-time: 2018/8/9-16:53
12  * desc: Create Successful!
13  **/
14 public class XMLGregorianCalendar {
15 
16     public static void main(String[] args) {
17         javax.xml.datatype.XMLGregorianCalendar xmlDate = null;
18         GregorianCalendar gc = new GregorianCalendar();
19         gc.setTime(new Date());
20 
21         try {
22             xmlDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
23             System.out.println("xmlDate-----:"+xmlDate.toString());
24             //To Remove TimeZone or "Z"
25             xmlDate.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
26             System.out.println("Without Time Zone-----:"+xmlDate.toString());
27 
28             //To Remove Time information
29             xmlDate.setTime(DatatypeConstants.FIELD_UNDEFINED,
30                     DatatypeConstants.FIELD_UNDEFINED,
31                     DatatypeConstants.FIELD_UNDEFINED,
32                     DatatypeConstants.FIELD_UNDEFINED);
33             System.out.println("Without Time Zone & Time :- " + xmlDate.toString());
34 
35         } catch (DatatypeConfigurationException e) {
36             e.printStackTrace();
37         }
38     }
39 
40 
41 }

 

posted @ 2018-08-09 16:59  念念不忘、  阅读(682)  评论(0编辑  收藏  举报