Calendar gmt0 = new GregorianCalendar(TimeZone.getTimeZone("GMT+0")); gmt0.setTimeInMillis(gmtlocal.getTimeInMillis()); System.out.println(gmt0.get(Calendar.YEAR));
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); sf.setTimeZone(TimeZone.getTimeZone("GMT+12")); System.out.println(sf.format(gmtlocal.getTime()));
Oracle 3. How to check the database time zone ? ======================================== Answer ------ The DBTIMEZONE SQL function returns the value of the database time zone.SQL> SELECT DBTIMEZONE FROM DUAL;DBTIMEZONE -------------- Europe/Lisbon@ Note that the database timezone is also included in both database_properties @ and props$ views: @ SQL> SELECT property_name, property_value @ FROM database_properties @ WHERE property_name='DBTIMEZONE'; @ and @ SQL> SELECT name, value$ @ FROM props$ @ WHERE name='DBTIMEZONE'; @ Be aware that you should not rely on these views because in case of db time zone @ change, these views reflect the new db time zone too early: they should reflect @ it only after database shutdown and restart.
谢谢sureyor() . 在oracle中,用 timestamp with time zone, 就可以了. 然后可以用TZH,TZM读取和保存timezone的信息. 现在不知道在JDBC中是如何从ResultSet中取得timezone的信息, 再查询中...
"JDBC中是如何从ResultSet中取得timezone的信息",不明白,SELECT DBTIMEZONE FROM DUAL这个SQL不行吗?
shan1119(大天使,卐~解!), 谢谢你给的信息!
sureyor(), 我的意思是在timestamp with time zone 这样的字段, 如何通过JDBC把保存的time zone信息拿到? 不是 DBTIMEZONE.
shan1119(大天使,卐~解!): ResultSet.getTimestamp().getTimezoneOffset(): The method getTimezoneOffset() from the type Date is deprecated. 这个方法不推荐了.我测试: 数据时间是: 01-12月-99 11.00.00.000000 上午 -08:00 System.out.println("getTimezoneOffset: " + ResultSet.getTimestamp().getTimezoneOffset().getTimezoneOffset() / 60); 打印出来是: getTimezoneOffset: -8结果是对的.
public static void main(String[] args) {
Calendar gmtlocal = new GregorianCalendar(TimeZone.getTimeZone("GMT+8"));
gmtlocal.set(Calendar.YEAR, 2007);
gmtlocal.set(Calendar.MONTH, 0);
gmtlocal.set(Calendar.DAY_OF_MONTH, 1);
gmtlocal.set(Calendar.HOUR_OF_DAY, 0);
gmtlocal.set(Calendar.MINUTE, 0);
Calendar gmt0 = new GregorianCalendar(TimeZone.getTimeZone("GMT+0"));
gmt0.setTimeInMillis(gmtlocal.getTimeInMillis());
System.out.println(gmt0.get(Calendar.YEAR));
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sf.setTimeZone(TimeZone.getTimeZone("GMT+12"));
System.out.println(sf.format(gmtlocal.getTime()));
sf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
System.out.println(sf.format(gmtlocal.getTime()));
sf.setTimeZone(TimeZone.getTimeZone("GMT+0"));
System.out.println(sf.format(gmtlocal.getTime()));
}
shan1119(大天使,卐~解!) : 就是夏令时把时间向后拨一个小时, 冬令时反之.这个是在国外, 所以还有这个问题.sureyor(). 谢谢, 我看下. 我的主要用途是在客户端的程序用户可以选择是DST还是不是. 然后把这个保存起来, 通过SQL语句把这些信息保存到数据库中.
3. How to check the database time zone ?
========================================
Answer
------
The DBTIMEZONE SQL function returns the value of the database time zone.SQL> SELECT DBTIMEZONE FROM DUAL;DBTIMEZONE
--------------
Europe/Lisbon@ Note that the database timezone is also included in both database_properties
@ and props$ views:
@ SQL> SELECT property_name, property_value
@ FROM database_properties
@ WHERE property_name='DBTIMEZONE';
@ and
@ SQL> SELECT name, value$
@ FROM props$
@ WHERE name='DBTIMEZONE';
@ Be aware that you should not rely on these views because in case of db time zone
@ change, these views reflect the new db time zone too early: they should reflect
@ it only after database shutdown and restart.
除非你在生成timestamp的时候,将当时的timezone也保存下来
ResultSet.getString(index) 这个可以把timezone的正确信息打印出来,比如:
1999-12-1 11.0.0.0 -8:0可是我想得到的是一个时间对象, 而不是一个字符串.
Calendar.setTime()
Calendar.setTimeZone()
Calendar.add(Calendar.DATE, 1);//or -1不知道会不会有问题.
ResultSet.getTimestamp().getTimezoneOffset(): The method getTimezoneOffset() from the type Date is deprecated. 这个方法不推荐了.我测试: 数据时间是: 01-12月-99 11.00.00.000000 上午 -08:00
System.out.println("getTimezoneOffset: " + ResultSet.getTimestamp().getTimezoneOffset().getTimezoneOffset() / 60);
打印出来是:
getTimezoneOffset: -8结果是对的.
返回的时间却还是-8, 也就是一直是本地时间. getTimezoneOffset() 这个是静态方法. :(
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@10.201.147.209:1521:chuden";
Connection conn= DriverManager.getConnection(url,"gyomu","gyomu");
Statement stmt=conn.createStatement();
StringBuffer sql = new StringBuffer();
sql.append(" select t,TO_char(t,'TZH:TZM') zone from tbltest");
ResultSet rs = stmt.executeQuery(sql.toString()); while(rs.next()){
Timestamp t = rs.getTimestamp("t");
String zone = rs.getString("zone");
Calendar c = Calendar.getInstance();
c.setTime(t);
TimeZone tz = TimeZone.getTimeZone("GMT"+zone);
c.setTimeZone(tz);
System.out.println(c.getTimeZone());
}
}catch(Exception e){
e.printStackTrace();
}
要是那样的话可以截取字符串,它的格式是一定的,应该能取得到.
String zone = rs.getString("t").substring(29);这块改成这个,就不用加那个sql了.
1999-12-1 11.0.0.0 -8:0这样可以取得timezone的信息, 可是这样去处理不好吧?