简体   繁体   中英

UTC Time is not getting stored in oracle column(TIMESTAMP WITH TIMEZONE) using JPA

I am getting UTC time in string format that we need to persist into database.

from UTC datetime string Calender object created using below code.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");

        OffsetDateTime dateTime = OffsetDateTime.parse(rqTime,formatter);

        System.out.println(dateTime);

        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        cal.setTime(Date.from(dateTime.toInstant()));

Then set above created calender instance to JPA

        EventDetails eventDetails = new EventDetails();
        eventDetails.setTimeWithZone(cal);
        event.insertEventDetails(eventDetails);

Entity class information

@Entity
@Table(name="mytimestamptz")
public class EventDetails implements Serializable{
    private static final long serialVersionUID = 1L;

@Column(name ="made_on" , columnDefinition = "TIMESTAMP WITH TIME ZONE")
@Type(type="com.elitecore.isl.bl.xlink.custom.UTCCalendarType")
private Calendar timeWithZone ; 

@Id
@SequenceGenerator(name = "generator", sequenceName = "SEQ_EVENTID", allocationSize = 1)
@Column(name ="id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generator")
private Long id;

This code insert UTC DAte String "2016-01-01T13:14:15+0000" as "01-JAN-2016 13:14:15 +0530" in database even if we have specified the timezone information (Refer UTCCalendarType).

UTCCalendarType created to store datetime in UTC format in database

public class UTCCalendarType extends CalendarType {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private static final TimeZone UTC = TimeZone.getTimeZone("UTC");

/**
 * This is the original code from the class, with two changes. First we pull
 * it out of the result set with an example Calendar. Second, we set the new
 * calendar up in UTC.
 */
@Override
public Object get(ResultSet rs, String name) throws SQLException {
    Timestamp ts = rs.getTimestamp(name, new GregorianCalendar(UTC));
    if (ts != null) {
        Calendar cal = new GregorianCalendar(UTC);
        cal.setTime(ts);
        return cal;
    } else {
        return null;
    }
}

@Override
public void set(PreparedStatement st, Object value, int index) throws SQLException {
    final Calendar cal = (Calendar) value;
    cal.setTimeZone(UTC);

    System.out.println("IST TIME : "+cal.getTime());

    st.setTimestamp(index, new Timestamp(cal.getTime().getTime()),Calendar.getInstance(UTC));

}

}

I am not getting whats going wrong in this code. why it is storing ASIA/KOLKATA TIMEZONE in database. Kindly provide valuable input on this.

Maybe it is just a problem of data representation: the date is stored correctly, but the DBMS (or the query browser) shows it in its local format.

You can test if it's the case by using specific functions (eg sys_extract_utc(timestamp) for ORACLE), or by comparing with some other valid stored date by query.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM