35

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?

Majid
  • 13,853
  • 15
  • 77
  • 113
Suniel
  • 1,449
  • 8
  • 27
  • 39

8 Answers8

68

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);
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
  • 3
    I can't see the getTime() method. Are you talking about a different package than http://docs.oracle.com/javase/7/docs/api/javax/xml/datatype/XMLGregorianCalendar.html ? – ilinca Aug 18 '15 at 16:04
  • 4
    @ilinca the getTime() method belongs to the GregorianCalendar Class. You need to parse XMLGregorianCalendar to GregorianCalendar using the .toGregorianCalendar() method as show above. – Mathter Sep 30 '15 at 15:58
9

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

David Pham
  • 1,673
  • 19
  • 17
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()));
ovpiNU
  • 29
  • 2
1

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() ) ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

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

schilippe
  • 79
  • 3
0

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

lalitbhagtani
  • 459
  • 5
  • 6
0

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;
}
Arundev
  • 1,388
  • 23
  • 22
0

This returns java.util.Date:

java.util.Date tempDate = issueDate.toGregorianCalendar().getTime();
Noman
  • 887
  • 1
  • 15
  • 34