Demir
Demir

Reputation: 1837

Java Read Date from Microsoft SQL Server Exactly without specifying TimeZone

I have DateTime field in my Microsoft SQL Server 2008 R2 database table. I read this table in my Android application by using jtds.jdbc.Driver successfully. But I have problem with reading correct DateTime value.

ResultSet Result = ...createStatement().executeQuery("SELECT * FROM Operation");

while (Result.next())
{
    Date iDate = this.getTimestamp(Result.getDate("Timestamp"), Result.getTime("Timestamp")));
}

public Date getTimestamp(java.sql.Date date, java.sql.Time time)
{
    return new Date (date.getTime() + time.getTime());
}

When I display iDate value I see that there is no timezone info. For example date in SQL server ends with time 16:51 however my diaplayed iDate has 14:51. Is there a way to get and display the exact DateTime value from the Microsoft SQL server without specifying any TimeZone value?

Upvotes: 0

Views: 336

Answers (1)

BDRSuite
BDRSuite

Reputation: 1612

It seems that you are working with different timezone. If the data you are saving in the database is different from your timezone, then it is likely the current time of your zone will be saved in the database. Hence you need to set the timestamp before adding it to your database. Please refer this post which clearly explains and can help you out.

Upvotes: 1

Related Questions