Java SQL Time getServerTimezone(Properties dbSettings)

Here you can find the source of getServerTimezone(Properties dbSettings)

Description

Returns the Server timezone.

License

Open Source License

Parameter

Parameter Description
dbSettings The database connection settings.

Exception

Parameter Description
SQLException an exception

Return

A TimeZone object.

Declaration

public static TimeZone getServerTimezone(Properties dbSettings) throws SQLException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) {2009,2011} {Software Design and Collaboration Laboratory (SDCL)
 *            , University of California, Irvine}.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:// ww  w  .  j a  v  a  2 s .c  o  m
 *    {Software Design and Collaboration Laboratory (SDCL)
 *   , University of California, Irvine} 
 *         - initial API and implementation and/or initial documentation
 *******************************************************************************/

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.Properties;
import java.util.TimeZone;

public class Main {
    private static TimeZone serverTimeZone;

    /**
     * Returns the Server timezone.
     * 
     * @param dbSettings
     *            The database connection settings.
     * @return A <code>TimeZone</code> object.
     * @throws SQLException
     */
    public static TimeZone getServerTimezone(Properties dbSettings) throws SQLException {
        if (serverTimeZone == null) {
            String url = dbSettings.getProperty("hibernate.connection.url");
            String username = dbSettings.getProperty("hibernate.connection.username");
            String password = dbSettings.getProperty("hibernate.connection.password");

            Connection conn = DriverManager.getConnection(url, username, password);

            ResultSet rs = conn.createStatement()
                    .executeQuery("select timestampdiff(HOUR,utc_timestamp(), current_timestamp());");
            String timediff = "";
            if (rs.next()) {
                timediff = rs.getString(1);
            }
            conn.close();

            serverTimeZone = TimeZone.getTimeZone("GMT" + timediff);
        }
        return serverTimeZone;
    }
}

Related

  1. getJoinedSysDateTime()
  2. getLastTimeOfDay(Calendar calendar)
  3. getMaxModifyTime(String path)
  4. getMaxPollTime(final Connection jdbcConnection, final String tableName)
  5. getServerTime()
  6. getSGWDateTime(long dateTime)
  7. getSQLDate(long datetime)
  8. getSQLDateTime()
  9. getZeroTimeDate(Date fecha)