当我在虚拟 Linux 服务器上安装 webapp 的 war 时,我遇到了以下问题。就是这样:我的服务器系统时间似乎是正确的,实际上在 shell 上输入日期会得到:
Mon Apr 11 11:47:30 CEST 2011
这是我的“挂钟时间”。即使 mysql 工作正常,如果我选择 now(),我得到:
mysql> select now()
-> ;
+---------------------+
| now() |
+---------------------+
| 2011-04-11 11:52:57 |
+---------------------+
1 row in set (0.01 sec)
但是我的应用程序(Java6 + tomcat 6 上的 Spring+hibernate)将使用 GMT 时区保存 DB 上的每个日期(并且时间正确偏移,与GMT,这很好)。现在的问题是,我得到的所有日期都以 GMT 显示,即使我这样做:
static final DateFormat format = new SimpleDateFormat(
"dd/MM/yyyy 'alle' HH:mm", Locale.ITALY);
我没有正确偏移小时,但它们仍保留在 GMT 中,并且如果我将 Z(显示时区)放入模式中,它们会显示 GMT。
相反,在 Windows 下,我将日期值存储在 mySQL 上的本机时区中。因此,在我开发的 Windows 机器上,我将拥有 CEST 中的所有日期。这很烦人,因为我不知道如何预测系统的行为方式,我必须进行无聊的测试并找出如何改变它。
你见过这个问题吗?怎么处理呢?
I've the following problem, that showed up when I installed my webapp's war on a virtual linux server. Here it is: my server system time seems correct, in fact typing date on shell comes out with:
Mon Apr 11 11:47:30 CEST 2011
which is my "wall clock time". Even mysql works fine, if I do select now(), I get:
mysql> select now()
-> ;
+---------------------+
| now() |
+---------------------+
| 2011-04-11 11:52:57 |
+---------------------+
1 row in set (0.01 sec)
But my application (Spring+hibernate on Java6 + tomcat 6) will save every date on DB with a GMT timezone (and the time is correctly offset with the difference from GMT, which is good). Now the problem is that I get all dates displayed with GMT, even if I do:
static final DateFormat format = new SimpleDateFormat(
"dd/MM/yyyy 'alle' HH:mm", Locale.ITALY);
I don't get the hours correctly offset, but they remain in GMT and they display GMT if I put Z (that displays timezone) in the pattern.
Under Windows instead I had date values stored in my native timezone on mySQL. So on my windows machine where I develop I will have all the dates in CEST. This is annoying as I don't know how to predict the way the system will behave and I have to do boring tests and figure out how to change this.
Do you have ever seen this problem? How to deal with it?
发布评论
评论(4)
DateFormat 确实支持时区的概念。添加如下内容:
DateFormat does support a notion of time zone. Add something like:
DateFormat
不关心时区,只关心格式。为了更改时区,您需要类似 Calendar.setTimezone()。DateFormat
does not care about time zones, only formatting. In order to change to timezone you need something like Calendar.setTimezone().准备语句 st = con
.prepareStatement("插入 knjiga_osisi (rb_knjiz,Inv_br,Naziv_os,datum_k,rb_pk,opis,ulaz,izlaz,stanje,osnovica_zam,otpis,sadasnjav, vrednost_pp,rashodovanav) 值 (?,?,?,STR_TO_DATE(?, '% d-%m-%Y'),?,?,?,?,?,?,?,?,?,?)");
这是java的代码。
Mysql - java 欧洲日期格式
此行用于日期
STR_TO_DATE(?, '%d-%m-%Y')
U 可以在 mysql 中仅测试日期行:
SELECT ID, DATE_FORMAT(Date, '%d-%M-% Y') 来自表1;
PreparedStatement st = con
.prepareStatement("insert into knjiga_osisi (rb_knjiz,Inv_br,Naziv_os,datum_k,rb_pk,opis,ulaz,izlaz,stanje,osnovica_zam,otpis,sadasnjav, vrednost_pp,rashodovanav) values (?,?,?,STR_TO_DATE(?, '%d-%m-%Y'),?,?,?,?,?,?,?,?,?,?)");
This is code for java.
Mysql - java european date format
This line is for date
STR_TO_DATE(?, '%d-%m-%Y')
U can test in mysql only date row:
SELECT ID, DATE_FORMAT(Date, '%d-%M-%Y') FROM table1;