Here you can find the source of getServerTimezone(Properties dbSettings)
Parameter | Description |
---|---|
dbSettings | The database connection settings. |
Parameter | Description |
---|---|
SQLException | an exception |
TimeZone
object.
public static TimeZone getServerTimezone(Properties dbSettings) throws SQLException
//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; } }