tag:blogger.com,1999:blog-865923359735383241.post839644096087376082..comments2023-10-29T07:27:09.012-06:00Comments on Ccna final exam - java, php, javascript, ios, cshap all in one: java.util.Date to XMLGregorianCalendarUnknownnoreply@blogger.comBlogger6125tag:blogger.com,1999:blog-865923359735383241.post-51056936599583778452012-06-11T22:04:59.887-06:002012-06-11T22:04:59.887-06:00I agree that it is better to use the getInstance m...I agree that it is better to use the getInstance method, but how can the "getInstance()" be faster than the constructor method? <br /><br />The getInstance() method calls the method "createCalendar(TimeZone zone, Locale aLocale)" which returns a "new GregorianCalendar(zone, aLocale)"!Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-42688008286922880082012-06-11T22:04:58.712-06:002012-06-11T22:04:58.712-06:00Just thought I'd add my solution below, since ...Just thought I'd add my solution below, since the answers above did not meet my exact needs. My Xml schema required seperate Date and Time elements, not a singe DateTime field. The standard XMLGregorianCalendar constructor used above will generate a DateTime field<br /><br />Note there a couple of gothca's, such as having to add one to the month (since java counts months from 0).<br /><br />GregorianCalendar cal = new GregorianCalendar();<br />cal.setTime(yourDate);<br />XMLGregorianCalendar xmlDate = df.newXMLGregorianCalendarDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH), 0);<br />XMLGregorianCalendar xmlTime = df.newXMLGregorianCalendarTime(cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND), 0);Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-76333623093181578542012-06-11T22:04:57.451-06:002012-06-11T22:04:57.451-06:00I hope my encoding here is right ;D
To make it fas...I hope my encoding here is right ;D<br />To make it faster just use the ugly getInstance() call of GregorianCalendar instead of constructor call:<br /><br /><br /><br />import java.util.GregorianCalendar;<br />import javax.xml.datatype.DatatypeFactory;<br />import javax.xml.datatype.XMLGregorianCalendar;<br /><br />public class DateTest {<br /><br /> public static void main(final String[] args) throws Exception {<br /> // do not forget the type cast :/<br /> GregorianCalendar gcal = (GregorianCalendar) GregorianCalendar.getInstance();<br /> XMLGregorianCalendar xgcal = DatatypeFactory.newInstance()<br /> .newXMLGregorianCalendar(gcal);<br /> System.out.println(xgcal);<br /> }<br /><br />}Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-71854456364296565102012-06-11T22:04:56.226-06:002012-06-11T22:04:56.226-06:00Here is a method for converting from a GregorianCa...Here is a method for converting from a GregorianCalendar to XMLGregorianCalendar; I'll leave the part of converting from a java.util.Date to GregorianCalendar as an exercise for you:<br /><br />import java.util.GregorianCalendar;<br /><br />import javax.xml.datatype.DatatypeFactory;<br />import javax.xml.datatype.XMLGregorianCalendar;<br /><br />public class DateTest {<br /><br /> public static void main(final String[] args) throws Exception {<br /> GregorianCalendar gcal = new GregorianCalendar();<br /> XMLGregorianCalendar xgcal = DatatypeFactory.newInstance()<br /> .newXMLGregorianCalendar(gcal);<br /> System.out.println(xgcal);<br /> }<br /><br />}<br /><br /><br />EDIT: Slooow :-)Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-89530362846242764732012-06-11T22:04:55.409-06:002012-06-11T22:04:55.409-06:00For those that might end up here looking for the o...For those that might end up here looking for the oposite convertion (XMLGregorianCalendar to Date):<br /><br />XMLGregorianCalendar xcal = <assume this is initialized>;<br />java.util.Date dt = xcal.toGregorianCalendar().getTime();Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-37878804576391500702012-06-11T22:04:54.378-06:002012-06-11T22:04:54.378-06:00GregorianCalendar c = new GregorianCalendar();
c.s...GregorianCalendar c = new GregorianCalendar();<br />c.setTime(yourDate);<br />XMLGregorianCalendar date2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.com