将 XMLGregorianCalendar 转换为 java.sql.Timestamp

2022-01-13 00:00:00 timestamp date java gregorian-calendar

我正在尝试将 XMLGregorianCalendar 日期分配给 java.sql.Timestamp var,就像这样...

I'm trying to assign a XMLGregorianCalendar date to a java.sql.Timestamp var, like this...

var1.setTimeStamp(Timestamp.valueOf(var2.getXMLGregorianCalendar().toString()))

但显然,这不起作用,并引发异常...

But apparently, this is not working, and throws an exception...

java.lang.IllegalArgumentException: 时间戳格式必须是 yyyy-mm-dd hh:mm:ss[.fffffffff]

java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]

我也试过这个:

var1.setTimeStamp((Timestamp) var2.getXMLGregorianCalendar().getTime())

但是……

java.lang.ClassCastException: java.util.Date 不能转换为 java.sql.Timestamp

java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Timestamp

任何想法..?谢谢!

推荐答案

我找到了答案:

    Timestamp timestamp = new Timestamp(var2.getXMLGregorianCalendar().toGregorianCalendar().getTimeInMillis());
    var1.setTimeStamp(timestamp);

相关文章