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?
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?
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()));
myPreparedStatement.setObject(
… ,
myXGC.toGregorianCalendar()
.toZonedDateTime()
) ;
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() ) ) ;
Use the DateFormat class in Java. This should be helpful: http://docs.oracle.com/javase/6/docs/api/java/text/DateFormat.html
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
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;
}
This returns java.util.Date:
java.util.Date tempDate = issueDate.toGregorianCalendar().getTime();