Convert XMLGregorianCalendar To Date I.e "MM/DD/YYYY Hh:mm:ss AM"


Answer :

You can do this to return a Date:



calendar.toGregorianCalendar().getTime()


I found that code from this tutorial. From there, you can use a SimpleDateFormat to turn it into a string in the format you want.



But, if you're using JDBC to save the date in the database, you probably can pass in the Date directly with this method:



preparedStatement.setDate(colNum, myDate);


Here is more clear answer:



Get instance of Date from XMLGregorianCalendar instance:



Date date = xmlCalendar.toGregorianCalendar().getTime();


I found that code from Convert XMLGregorianCalendar to Date in Java



Format that Date instance with format "MM/dd/yyyy hh:mm:ss a", you will get MM/DD/YYYY hh:mm:ss AM format



DateFormat  formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
String formattedDate = formatter.format(date)


From Convert Date to String in Java



For inserting database you would do what Daniel suggested



If you want to insert your date on a database I would first do what Daniel suggested:



XMLGregorianCalendar xgc=<assume this is initialized>;
Date timestamp=xgc.toGregorianCalendar().getTime();


and later insert it through a PreparedStatement as Timestamp in milliseconds (Epoch time). That way you won't loose precision.



preparedStatement.setTimestamp(colNum,new Timestamp(timestamp.getTime()));


Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android SDK Location Should Not Contain Whitespace, As This Cause Problems With NDK Tools