Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Convert .NET DateTimeOffset <--> Jodatime DateTime (utc)(How can I convert between these kinds of time in EclipseLink?)
Convert .NET DateTimeOffset <--> Jodatime DateTime (utc) [message #1053013] Wed, 01 May 2013 23:03 Go to next message
Michael Gardner is currently offline Michael GardnerFriend
Messages: 4
Registered: April 2013
Junior Member
Hello,

I am creating a RESTful service with JAX-RS, JPA2, and Jodatime. We are using an MS SQL Server database. There is a database table that contains a .NET DateTimeOffset column for an expiration date. It contains the date and time value expressed in the UTC timezone (zero offset). I have an entity that represents this table. When I GET a list from this table, I am receiving the following exception:

javax.servlet.ServletException: java.lang.ClassCastException: microsoft.sql.DateTimeOffset cannot be cast to java.sql.Timestamp

I have found information about creating a converter, but it does not seem to resolve this issue. It converts between Timestamp and Jodatime DateTime values. I cannot find any information about how to convert between DateTimeOffset and Timestamp or Jodatime DateTime types. I would appreciate your suggestions so I can move forward on our project.

Thank you for your time,

Mike

    // Assigning the converter to the Jodatime DateTime variable.
    @Column(name = "Expiration")
    @Converter(name = "dateTimeConverter", 
        converterClass =  JodaDateTimeConverter.class)
    @Convert("dateTimeConverter")
    private DateTime expiration;


// Converter code from the web...
import java.sql.Timestamp;
import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.mappings.converters.Converter;
import org.eclipse.persistence.mappings.foundation.AbstractDirectMapping;
import org.eclipse.persistence.sessions.Session;
import org.joda.time.DateTime;

/**
 *
 * @author Dev
 */
public class JodaDateTimeConverter implements Converter {

    private static final long serialVersionUID = 1L;

    @Override
    public Object convertDataValueToObjectValue(Object dataValue, Session session) {
        return dataValue == null ? null : new DateTime((Timestamp) dataValue);
    }

    @Override
    public Object convertObjectValueToDataValue(Object objectValue, Session session) {
        return objectValue == null ? null : new Timestamp(((DateTime) objectValue).getMillis());
    }

    @Override
    public void initialize(DatabaseMapping mapping, Session session) {
        ((AbstractDirectMapping) mapping)
            .setFieldClassification(java.sql.Timestamp.class);
    }

    @Override
    public boolean isMutable() {
        return false;
    }
}
Re: Convert .NET DateTimeOffset <--> Jodatime DateTime (utc) [message #1053185 is a reply to message #1053013] Thu, 02 May 2013 23:50 Go to previous messageGo to next message
Michael Gardner is currently offline Michael GardnerFriend
Messages: 4
Registered: April 2013
Junior Member
Hello Everyone,

I could not find a clean way to convert between a Microsoft DateTimeOffset value and a Joda-Time DateTime value. My plan was to have the converter change the DateTimeOffset to an ISO-8601 string, and then create a DateTime from it. Unfortunately, I could not find any way to convert a DateTimeOffset to anything in Java. Sad So, I've changed our database to save the expiration as an ISO-8601 string. It does require more storage space, but it is infinitely more portable than binary values. Next, I created a few transient properties on my class in which I have the getters do some conversions.

Hope this info helps someone... If anyone knows how to convert a Microsoft DateTimeOffset to a Joda-Time DateTime in Java, please let me know. Smile

Mike
Re: Convert .NET DateTimeOffset <--> Jodatime DateTime (utc) [message #1053780 is a reply to message #1053185] Tue, 07 May 2013 14:49 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

DateTimeOffset seems to define a getTimestamp() method, and a valueOf(Timestamp) method. You can use these to convert to a Timestamp, then convert the Timestamp to Joda DateTime.

You could also use a regular date-time type in SQL Server.


James : Wiki : Book : Blog : Twitter
Previous Topic:Problem mongodb query/find by id
Next Topic:Is UNION supported in a subquery?
Goto Forum:
  


Current Time: Mon Dec 22 23:44:41 GMT 2014

Powered by FUDForum. Page generated in 0.01674 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software