4

I have a scenario where I have to convert java.sql.Date to class javax.xml.datatype.XMLGregorianCalendar and I tried the following

GregorianCalendar cal = new GregorianCalendar();
cal.setTime(financialTransactionProcessDataObj.invReceivedDate);
DatatypeFactory dTF = DatatypeFactory.newInstance();
XMLGregorianCalendar date = dTF.newXMLGregorianCalendar(cal);

I am getting

java.lang.SecurityException: Restricted method invocation. for DatatypeFactory

Is there any other way to do this?

Siva
  • 289
  • 1
  • 6
  • 16
  • 1
    Duplicate of https://stackoverflow.com/questions/835889/java-util-date-to-xmlgregoriancalendar. – ewramner Mar 14 '18 at 08:29
  • It seems that your code is correct. I suppose that the problem is related to financialTransactionProcessDataObj.invReceivedDate. Can you check if replacing it with new java.util.Date() it works? – Davide Lorenzo MARINO Mar 14 '18 at 08:34
  • @user2612030 I know its a duplicate but I am seeing the java.lang.SecurityException: Restricted method invocation. for DatatypeFactory, so is there any other way to do this – Siva Mar 14 '18 at 08:38
  • @DavideLorenzoMARINO no it didnt I am seeing java.lang.SecurityException: Restricted method invocation. for DatatypeFactory for the script I am writing, so I just wanted to know is there any other way – Siva Mar 14 '18 at 08:41
  • 1
    IN what environment are you working? (tomcat? jboss? jar standalone? something different?) – Davide Lorenzo MARINO Mar 14 '18 at 08:46
  • @DavideLorenzoMARINO something different. – Siva Mar 14 '18 at 08:47
  • 1
    Try to search for specific security restrictions related to your environment. Or post informations related to it to see if somebody can help you here. – Davide Lorenzo MARINO Mar 14 '18 at 08:50
  • @user2612030 What is the difference between the accepted answer to the question you are linking to compared to the code in this question? I cannot see how that question would help. – Ole V.V. Mar 14 '18 at 09:01
  • 1
    Siva, you may want to post a complete stack trace. – Ole V.V. Mar 14 '18 at 09:03
  • 1
    You may not need an `XMLGregorianCalendar`. If this is for formatting your date for use in XML, what you need is ISO 8601 format, and the classes of [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) produce that format through their `toString` methods. – Ole V.V. Mar 14 '18 at 09:05
  • 3
    Possible duplicate of [java.util.Date to XMLGregorianCalendar](https://stackoverflow.com/questions/835889/java-util-date-to-xmlgregoriancalendar) – daniu Mar 14 '18 at 20:44
  • My 0.02 cents: This question is not a duplicate since the OP is already trying what it suggests in the answers to the linked question, so those answer won’t help any further. And not all errors can be readily reproduced everywhere. Asking about them is still OK and not a reason to close the question. – Ole V.V. Mar 15 '18 at 12:11

1 Answers1

1

I assume that your desired result is an XMLGregorianCalendar like 2018-06-30T00:00:00.000+02:00 (at least that’s the result I get when running your code on my computer (it works)).

This answer further assumes that you are getting your java.sql.Date from an SQL database and that you want your XMLGregorianCalendar for the String that you get from its toXMLFormat method. If so, I suggest that you neither need java.sql.Date nor XMLGregorianCalendar (that’s right).

To get your date from your result set:

    LocalDate invReceivedDate = yourResultSet.getObject("inv_receive_date", LocalDate.class);

I haven’t tested the code, and you should of course substitute the correct column label instead of inv_receive_date. If using JPA a modern version can give you a LocalDate object too.

To get your string for XML:

    OffsetDateTime date = invReceivedDate.atStartOfDay(ZoneId.systemDefault())
            .toOffsetDateTime();
    System.out.println(date.toString());

In my test run this printed

2018-06-30T00:00+02:00

If you compare with the string I got from date.toXMLFormat() before, you will notice that the seconds and fraction of second are not printed. This is probably OK. The format is ISO 8601, and according to that standard seconds and fraction of second are optional, so leaving them out when they are 0 should work. If you have a special requirement to include them, you may use

    DateTimeFormatter formatterForXml
            = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSxxx");
    System.out.println(date.format(formatterForXml));

This time the output is

2018-06-30T00:00:00.000+02:00

So exactly as from date.toXMLFormat() before. It’s also possible that you only need the date without time-of-day and UTC offset. If so, it still simpler:

    System.out.println(invReceivedDate.toString());

Output:

2018-06-30

Why? Not just because it works. java.sql.Date is long outdated and poorly designed, so don’t use it. Instead I am using LocalDate from java.time, the modern Java date and time API. The modern API is so much nicer to work with.

Link: Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161