我有包含用户表的表的MonetDB数据库:
CREATE TABLE user
(
birth_date TIMESTAMP NOT NULL
);
birth_date
在没有DST的情况下保存在GMT中。 (这是MonetDB的默认行为)。所以我应该更改我的应用程序中的TimeZone
。这是我的代码:
Class.forName("nl.cwi.monetdb.jdbc.MonetDriver");
Connection con = DriverManager.getConnection("jdbc:monetdb://localhost/online", "monetdb", "monetdb");
Statement st = con.createStatement();
ResultSet rs;
rs = st.executeQuery("SELECT * FROM user");
while (rs.next()) {
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("Asia/Tehran"));
c.setTime(rs.getTimestamp("birth_date"));
System.out.println(c.get(Calendar.YEAR) + "-" + c.get(Calendar.MONTH) + "-" + c.get(Calendar.DAY_OF_MONTH) + " " + c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND));
}
但是此代码在数据库中打印相同的TIMESTAMP
。这是转换TimeZone
的错误方法吗?我在Debian 6上使用MonetDB版本11.9.5-20120516和openjdk 6.这是monetdbd getall /home/dbfarm
:
dbfarm /home/dbfarm/
status monetdbd[4187] 1.6 (Apr2012-SP1) is serving this dbfarm
mserver /usr/bin/mserver5
logfile /home/dbfarm//merovingian.log
pidfile /home/dbfarm//merovingian.pid
sockdir /tmp
port 50000
exittimeout 60
forward proxy
discovery yes
discoveryttl 600
control yes
passphrase {SHA512}ba3253876aed6bc22d4a6ff53d8406c6ad864195ed144ab5c87621b6c233b548baeae6956df346ec8c17f5ea10f35ee3cbc514797ed7ddd3145464e2a0bab413
mapisock /tmp/.s.monetdb.50000
controlsock /tmp/.s.merovingian.50000
答案 0 :(得分:1)
您如何确定从monetdb返回的时间?
我使用了以下代码,它使用Date
而不是java.sql.Timestamp
,并且它适当地从一个时区转换为另一个时区,因此有可能将某些时间从UTC转换为本地时间为你自动?
Date d = new Date();
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("Asia/Tehran"));
c.setTime(d);
System.out.println(c.get(Calendar.YEAR) + "-" + c.get(Calendar.MONTH) + "-" + c.get(Calendar.DAY_OF_MONTH) + " " + c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND));
c.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(c.get(Calendar.YEAR) + "-" + c.get(Calendar.MONTH) + "-" + c.get(Calendar.DAY_OF_MONTH) + " " + c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND));
编辑如果您的java配置错误,尝试获取时区可能会失败;在与GMT相同的时区产生输出,所以你应该总是确保你得到正确的时区
TimeZone tz = TimeZone.getTimeZone("Asia/Tehran");
System.out.println(tz.toString());
为我显示sun.util.calendar.ZoneInfo[id="Asia/Tehran",offset=12600000,dstSavings=3600000,useDaylight=true,transitions=100,lastRule=java.util.SimpleTimeZone[id=Asia/Tehran,offset=12600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=1,startMonth=2,startDay=21,startDayOfWeek=0,startTime=0,startTimeMode=0,endMode=1,endMonth=8,endDay=21,endDayOfWeek=0,endTime=0,endTimeMode=0]]
;而:
TimeZone tz = TimeZone.getTimeZone("Asia/fred");
System.out.println(tz.toString());
显示sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
答案 1 :(得分:0)
最后我得到了答案。
默认情况下,MonetDB在GMT
中运行,因为Asia/Tehran
在数据库配置中不可用,所以我需要在我的应用程序中将时间转换为Asia/Tehran
时区。 JVM从OS获取其默认TimeZone
,我的操作系统设置为Asia/Tehran
。因此,该程序默认在Asia/Tehran
运行,并在代码中再次将其更改为“Asia / Tehran”并没有任何好处。我做的是这个:
TimeZone
更改为UTC
。TimeZone
更改为Asia/Tehran
这是代码:
/* Change the default TimeZone of JVM */
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
Calendar c = Calendar.getInstance();
Class.forName("nl.cwi.monetdb.jdbc.MonetDriver");
Connection con = DriverManager.getConnection("jdbc:monetdb://localhost/online", "monetdb", "monetdb");
Statement st = con.createStatement();
ResultSet rs;
rs = st.executeQuery("SELECT * FROM user");
/* In each iteration:
* 1. set TimeZone to UTC
* 2. fetch record
* 3. convert to Asia/Tehran and so on ... */
while (rs.next()) {
c.setTimeZone(TimeZone.getTimeZone("UTC"));
c.setTime(rs.getTimestamp("birth_date"));
System.out.println(c); /* This is original values in the database */
c.setTimeZone(TimeZone.getTimeZone("Asia/Tehran"));
System.out.println(c); /* This is the values I'm looking for */
}
请注意,如果没有TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
,这将无效。因为JDBC已连接到数据库并将时间转换为JVM的默认TimeZone
(在本例中为Asia/Tehran
),然后才能进行任何转换。