convert XMLGregorianCalendar to date i.e “MM/DD/YYYY hh:mm:ss AM”

陌路散爱 提交于 2019-12-04 15:15:12

问题


I have a date in XMLGregorianCalendar format like "2013-05-16T09:54:13" which i have to convert to timestamp "MM/DD/YYYY hh:mm:ss AM" for inserting into oracle database table using java.

how can i do this in Java?


回答1:


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);



回答2:


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




回答3:


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()));



回答4:


tl;dr

myPreparedStatement.setObject( 
    … , 
    myXGC.toGregorianCalendar()
         .toZonedDateTime()
) ;

java.time

The modern approach uses the java.time classes that supplanted the troublesome old classes Date, Calendar, and GregorianCalendar.

Convert from the legacy classes to java.time.

GregorianCalendar gc = myXGC.toGregorianCalendar() ;
ZonedDateTime zdt = gc.toZonedDateTime();

Pass date-time values to your database as date-time objects rather than as strings.

If your JDBC driver complies with JDBC 4.2 and later, you can deal directly with java.time types.

myPreparedStatement.setObject( … , zdt ) ;

If your driver is not yet compliant, convert briefly to a java.sql type.

myPreparedStatement.setTimestamp( … , java.sql.Timestamp.from ( zdt.toInstant() ) ) ;



回答5:


Use the DateFormat class in Java. This should be helpful: http://docs.oracle.com/javase/6/docs/api/java/text/DateFormat.html




回答6:


You can convert XMLGregorianCalendar to java.util.Date Object by using these two lines of code :-

Date date = xmlDate.toGregorianCalendar().getTime();
System.out.println("java.util.date :- " + date);

To convert to java.slq.Date Object use this code :-

long time = xmlDate.toGregorianCalendar().getTime().getTime();
java.sql.Date sqlDate = new java.sql.Date(time);

You can see complete example here




回答7:


Please use the function below - just pass the XMLGregorianCalendar instance and date format you want (format example: "DD MMMM yyyy" -> 01 January 2017)

public String parseDate(String format,XMLGregorianCalendar XMLdate){
    Date date = XMLdate.toGregorianCalendar().getTime();
    DateFormat  formatter = new SimpleDateFormat(format);
    String formattedDate  = formatter.format(date);
    return formattedDate;
}



回答8:


This returns java.util.Date:

java.util.Date tempDate = issueDate.toGregorianCalendar().getTime();


来源:https://stackoverflow.com/questions/16706431/convert-xmlgregoriancalendar-to-date-i-e-mm-dd-yyyy-hhmmss-am

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!