获取系统时间后,和本机时间差8小时的问题

时间:2008-06-19 11:29:20   来源:论坛整理  作者:  编辑:chinaitzhe
我的代码是:
<%
java.SimpleDateFormat formatter = new java.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

java.util.Date currentTime = new java.util.Date();//得到当前系统时间

String str_date1 = formatter.format(currentTime); //将日期时间格式化
String str_date2 = currentTime.toString(); //将Date型日期时间转换成字符串形式

out.println(currentTime);
%>
但是输出的是比我本机时间提前8小时啊,如何能改成本机时间呢??我查看了本机的时区设置的也是对的啊~~
网友回复:楼主,我顶你,因为,我现在也碰到了这个问题,我想自己做个自动关机软件自己用,结果,每次设置倒计时,明明传进去的是30000毫秒,也就是30秒,但是它倒计时总是从8小时零30秒开始,始终有8个小时,不知道为什么,搞的我只有传进去参数之后再减8个小时·······
网友回复:String str_date1 = formatter.format(currentTime); //将日期时间格式化
= currentTimes.format(formatter);
网友回复:楼主 你的程序不报错了么? 我刚才调试了一下:

java.SimpleDateFormat formatter = new java.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

应该改为:

java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

结果是正确的,请看:

http://www.javawind.net/test.jsp

Thu Jun 12 17:21:55 CST 2008
网友回复:真是神气,本身获取的时间就应该是系统时间啊?怎么会不一样~?也许是你的系统时钟有问题...


网友回复:这肯定是你们电脑的问题

点开系统时间
把时区设置成北京8点
在Internet时间,勾上“自动与Internet时间服务器同步”,下拉列表选择“time.windows.com”试试


再不行,换个TOMCAT
网友回复:时区原因?
网友回复:
HTML code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





<%@ page language="java" contentType="text/html; charset=gb2312"

    pageEncoding="gb2312"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>

<title>JavaWind Loading...</title>

</head>

<body>

<% 

java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 



java.util.Date currentTime = new java.util.Date();//得到当前系统时间 



String str_date1 = formatter.format(currentTime); //将日期时间格式化 

String str_date2 = currentTime.toString(); //将Date型日期时间转换成字符串形式 



out.println("currentTime = " currentTime   "<br/>"); 

out.println("str_date1   = " str_date1  "<br/>"); 

out.println("str_date2   = " str_date2  "<br/>"); 

%>

</body>

</html>





http://www.javawind.net/test.jsp
网友回复:楼主,你这样搞一下试试。

Java code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





  DateFormat dateFormatterChina = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM);//格式化输出

  TimeZone timeZoneChina = TimeZone.getTimeZone("Asia/Shanghai");//获取时区 这句加上,很要害。

  dateFormatterChina.setTimeZone(timeZoneChina);//设置系统时区

  Date curDate = new Date();//获取系统时间




网友回复:好~~谢谢~~
网友回复:感觉是时区问题
把时区设置下试试看
网友回复:我晕,说调时区的.....这个类SimpleDateFormat在哪个包下面?这段能编译过吗?我记得是java.text.SimpleDateFormat吧
网友回复:按照8楼的做法,自己指定时区吧!
网友回复:tomcat启动脚本中配置jvm参数 -Duser.timezone=GMT 08
网友回复:指定时区。。。

网友回复:8楼的方法最好

网友回复:
引用 8 楼 TRMeister 的回复:
楼主,你这样搞一下试试。


Java code
DateFormat dateFormatterChina = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM);//格式化输出
TimeZone timeZoneChina = TimeZone.getTimeZone("Asia/Shanghai");//获取时区 这句加上,很要害。
dateFormatterChina.setTimeZone(timeZoneChina);//设置系统时区
Date curDate = new Date();//获取系统时间


我顶你..
网友回复:第一次听说这种问题,学习了
关键字:获取,系统,时间,时间差,小时,问题,

文章评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面