Обсуждение: How to properly convert PostgreSQL timestamp to Java xsd:dateTime

Поиск
Список
Период
Сортировка

How to properly convert PostgreSQL timestamp to Java xsd:dateTime

От
"Campbell, Lance"
Дата:

PostgreSQL 9.5.4

Java 1.8

JDBC driver postgresql-9.4.1208.jar

 

Issue:

I need to map a PostgreSQL timestamp to a field in a Java bean.  The Java bean was generated using standard XSD with the element type of xsd:dateTime.  The Class type Java is assigning to this field in the Bean class is XMLGregorianCalendar.

 

Question:

I don’t know the proper way to “get” the value from the result set and the populate the Java bean.  Your assistants would be very helpful.

 

Process I have tried:

In my Java XSD I have an element defined within a complexType:

 

<xsd:element name="startTimestamp" type="xsd:dateTime" minOccurs="0" />

 

I next generate the bean.  It generates this code in Java for the above field:

 

protected XMLGregorianCalendar startTimestamp;

 

public XMLGregorianCalendar getStartTimestamp()

{

        return startTimestamp;

}

 

public void setStartTimestamp(XMLGregorianCalendar value)

{

        this.startTimestamp = value;

}

 

In PostgrSQL I have this column in table_a:

 

start_timestamp timestamp with time zone DEFAULT now(),

 

I then select the data from the database in Java:

 

String sqlStatement = “select start_timestamp from table_a”;

resultObject.setStartTimestamp(resultSet.getTimestamp("start_timestamp");

 

I get this error:

The method setStartTimestamp(XMLGregorianCalendar) in the type MyObject is not applicable for the arguments (Timestamp)

 

 

Thanks,

 

Lance

Re: How to properly convert PostgreSQL timestamp to Java xsd:dateTime

От
Christian Castelli
Дата:

Maybe something like this snippet taken from stackoverflow:

GregorianCalendar c = new GregorianCalendar(); c.setTime(yourDate); XMLGregorianCalendar date2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);

where your date it's taken from the resultset.

Il 08/giu/2016 17:36, "Campbell, Lance" <lance@illinois.edu> ha scritto:

PostgreSQL 9.5.4

Java 1.8

JDBC driver postgresql-9.4.1208.jar

 

Issue:

I need to map a PostgreSQL timestamp to a field in a Java bean.  The Java bean was generated using standard XSD with the element type of xsd:dateTime.  The Class type Java is assigning to this field in the Bean class is XMLGregorianCalendar.

 

Question:

I don’t know the proper way to “get” the value from the result set and the populate the Java bean.  Your assistants would be very helpful.

 

Process I have tried:

In my Java XSD I have an element defined within a complexType:

 

<xsd:element name="startTimestamp" type="xsd:dateTime" minOccurs="0" />

 

I next generate the bean.  It generates this code in Java for the above field:

 

protected XMLGregorianCalendar startTimestamp;

 

public XMLGregorianCalendar getStartTimestamp()

{

        return startTimestamp;

}

 

public void setStartTimestamp(XMLGregorianCalendar value)

{

        this.startTimestamp = value;

}

 

In PostgrSQL I have this column in table_a:

 

start_timestamp timestamp with time zone DEFAULT now(),

 

I then select the data from the database in Java:

 

String sqlStatement = “select start_timestamp from table_a”;

resultObject.setStartTimestamp(resultSet.getTimestamp("start_timestamp");

 

I get this error:

The method setStartTimestamp(XMLGregorianCalendar) in the type MyObject is not applicable for the arguments (Timestamp)

 

 

Thanks,

 

Lance

Re: How to properly convert PostgreSQL timestamp to Java xsd:dateTime

От
Vladimir Sitnikov
Дата:
Lance, 

The column name "start_timestamp" suggests that you are storing "point-in-time" kind of timestamps.
The question is: is it "timestamp with time zone" or "timestamp without time zone"? I think "with time zone" makes things easier.

If the field is "with time zone", then 
GregorianCalendar c = new GregorianCalendar();
Timestamp ts = resultSet.getTimestamp("start_timestamp", c); // <-- you'd better always use calendar-aware methods
c.setTime(ts);
XMLGregorianCalendar xmlStartTimestamp = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);

... setStartTimestamp(xmlStartTimestamp)


If performance matters, you might want to cache GregorianCalendar & DatatypeFactory objects outside of the loop (however note that neither of them is thread-safe).

Vladimir

Re: How to properly convert PostgreSQL timestamp to Java xsd:dateTime

От
Christian Castelli
Дата:

Maybe something like this snippet taken from stackoverflow:

GregorianCalendar c = new GregorianCalendar(); c.setTime(yourDate); XMLGregorianCalendar date2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);

where your date it's taken from the resultset.

Il 08/giu/2016 17:36, "Campbell, Lance" <lance@illinois.edu> ha scritto:

PostgreSQL 9.5.4

Java 1.8

JDBC driver postgresql-9.4.1208.jar

 

Issue:

I need to map a PostgreSQL timestamp to a field in a Java bean.  The Java bean was generated using standard XSD with the element type of xsd:dateTime.  The Class type Java is assigning to this field in the Bean class is XMLGregorianCalendar.

 

Question:

I don’t know the proper way to “get” the value from the result set and the populate the Java bean.  Your assistants would be very helpful.

 

Process I have tried:

In my Java XSD I have an element defined within a complexType:

 

<xsd:element name="startTimestamp" type="xsd:dateTime" minOccurs="0" />

 

I next generate the bean.  It generates this code in Java for the above field:

 

protected XMLGregorianCalendar startTimestamp;

 

public XMLGregorianCalendar getStartTimestamp()

{

        return startTimestamp;

}

 

public void setStartTimestamp(XMLGregorianCalendar value)

{

        this.startTimestamp = value;

}

 

In PostgrSQL I have this column in table_a:

 

start_timestamp timestamp with time zone DEFAULT now(),

 

I then select the data from the database in Java:

 

String sqlStatement = “select start_timestamp from table_a”;

resultObject.setStartTimestamp(resultSet.getTimestamp("start_timestamp");

 

I get this error:

The method setStartTimestamp(XMLGregorianCalendar) in the type MyObject is not applicable for the arguments (Timestamp)

 

 

Thanks,

 

Lance

Re: How to properly convert PostgreSQL timestamp to Java xsd:dateTime

От
Vladimir Sitnikov
Дата:
Lance, 

The column name "start_timestamp" suggests that you are storing "point-in-time" kind of timestamps.
The question is: is it "timestamp with time zone" or "timestamp without time zone"? I think "with time zone" makes things easier.

If the field is "with time zone", then 
GregorianCalendar c = new GregorianCalendar();
Timestamp ts = resultSet.getTimestamp("start_timestamp", c); // <-- you'd better always use calendar-aware methods
c.setTime(ts);
XMLGregorianCalendar xmlStartTimestamp = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);

... setStartTimestamp(xmlStartTimestamp)


If performance matters, you might want to cache GregorianCalendar & DatatypeFactory objects outside of the loop (however note that neither of them is thread-safe).

Vladimir