我的一个jsp文件。
里面使用
out.println(new java.sql.Timestamp(new java.util.Date().getTime()));
得到的时间和实际系统的时间不一样,而且还相差10多个小时。系统是linux的FC5
Tomcat用的是5.5.12
j2sdk用的是1.5.0_06的linux下用date命令得到时间是正确的
mysql中使用select now();显示的时间也是正确的奇怪了,请问这个问题该如何解决??应该是jdk或者tomcat上的设置。
里面使用
out.println(new java.sql.Timestamp(new java.util.Date().getTime()));
得到的时间和实际系统的时间不一样,而且还相差10多个小时。系统是linux的FC5
Tomcat用的是5.5.12
j2sdk用的是1.5.0_06的linux下用date命令得到时间是正确的
mysql中使用select now();显示的时间也是正确的奇怪了,请问这个问题该如何解决??应该是jdk或者tomcat上的设置。
我直接在服务器里面使用命令,查看服务器的时间,是正确的,比如说是:14:43分。
而且,我又进入服务器里面的mysql,然后用select now();得到的时间也差不多是14:43分。偏偏放在tomcat里面的jsp文件,out.println(new java.util.Date());得到的时间是
2006-05-23 06:42:50非常奇怪!!!但是,我在我自己的电脑上,用这个jsp文件,得到的时间又是对的,差不多也是14:43分。
(GMT+08:00)北京,重庆,香港特别行政区,乌鲁木齐
所以初步判断是出在tomcat或者jdk上面。不知道那里有这个设置?
所以,我需要在写入数据库的时候,把它变成正确的。用Locale这个没有用,因为数据库里面已经不是当前时间
import java.text.*;
引入上述Date date=new Date();
DateFormat fmt=DateFormat.getDateInstance(DateFormat.FULL,Local.CHINA);
//dataformat是指示时间的格式和时区(local.CHINA)
String formatted= fmt.format(date);//formatted就是最后要得到的时间
DateFormat fmt=DateFormat.getDateInstance(DateFormat.FULL,Local.CHINA);
String formatted =fmt.format(date);
DateFormat fmt=DateFormat.getDateInstance(DateFormat.FULL,Local.CHINA);
//dataformat是指示时间的格式和时区(local.CHINA)
String formatted= fmt.format(date);//formatted就是最后要得到的时间------------------------
Locale.CHINA 和 Locale.US
只是时间显示的方式不一样。
两者是一样的啦。。
在得到或者生成时间的时候有根据时区而生成不一样的时间嘛?
DateFormat gmt08Formatter=DateFormat.getDateTimeInstance();
TimeZone timezone=TimeZone.getTimeZone("GMT+08:00");
gmt08Formatter.setTimeZone(timezone);
//获得格式化后的东八区时间
String gmt08DateTime=gmt08Formatter.format(now);
System.out.println(gmt08DateTime);这又是为什么?操作系统时区明明是GMT+08(东八区)为什么用上面的方法不能得到系统时间?
DateFormat f2=DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM,Locale.US);
DateFormat f3=DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM,Locale.KOREA);
Date now=new Date();
System.out.println(f.format(now));
System.out.println(f2.format(now));
System.out.println(f3.format(now));结果如下:
2007-4-11 5:19:04
Apr 11, 2007 5:19:04 AM
2007. 4. 11 ?? 5:19:04
(最末一行??是未安装韩语不能正常显示)
从结果看,不管用什么Locale,都不能改变输出的时间.