
public Date getCST(String strGMT) throws ParseException {
DateFormat df = new SimpleDateFormat("EEE, d-MMM-yyyy HH:mm:ss z", LocaleENGLISH);
return dfparse(strGMT);
}
public String getGMT(Date dateCST) {
DateFormat df = new SimpleDateFormat("EEE, d-MMM-yyyy HH:mm:ss z", LocaleENGLISH);
dfsetTimeZone(TimeZonegetTimeZone("GMT")); // modify Time Zone
return(dfformat(dateCST));
}
2种方法仅供参考,希望 采纳。
我假设了你的已知时间类型为Calendar,如果不是你也可以自己改成Date类型,代码如下:
import javatextDateFormat;import javatextSimpleDateFormat;
import javautilCalendar;
import javautilTimeZone;
public class Test {
public static void main(String[] args) {
//假如这个是你已知的时间类型
Calendar cal = CalendargetInstance();
calgetTimeInMillis();
//北京时区GMT+8
Calendar beijingcal = CalendargetInstance();
beijingcalclear();
beijingcalsetTimeZone(TimeZonegetTimeZone("GMT+8"));
beijingcalsetTimeInMillis(calgetTimeInMillis());
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String beijingFormatStr = fmtformat(beijingcalgetTime());
Systemoutprintln(beijingFormatStr);
}
}
可以通过修改mycnf 在 [mysqld] 之下加 default-time-zone=timezone 来修改时区。如: default-time-zone = '+8:00' 修改完了记得记得重启msyql 注意一定要在 [mysqld] 之下加 ,否则会出现 unknown variable 'default-time-zone=+8:00'!
在一个精简的Linux安装上jre,发现所有的java程序获取的时间都不是系统时间。后来发现原来是时区不对。
java程序获取的时间都是GMT时间,而系统是北京时间,应该是GMT+8,刚好相差8小时。网上搜了好多资料,只有两种方法:1、在程序中使用
java的函数设定时区。2、在启动java程序时加参数-Dusertimezone=GMT+8
你装的是XP系统吧
这是XP的一个补丁造成的
改补丁修改了注册表中对时区的设置致使java获取时间错误
你把周围人的注册表(HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation)
copy一份到你的电脑上,你的注册表有问题。
或者把一下代码复制到记事本上,后缀改为:1reg,然后双击,点是。
如果还不行请联系我。 -------------
Windows Registry Editor Version 500
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation]
"Bias"=dword:fffffe20
"StandardName"="中国标准时间"
"StandardBias"=dword:00000000
"StandardStart"=hex:00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00
"DaylightName"="中国标准时间"
"DaylightBias"=dword:00000000
"DaylightStart"=hex:00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00
"ActiveTimeBias"=dword:fffffe20
以上就是关于java 时区转换全部的内容,包括:java 时区转换、java时间转换,带时区的、mysql-connector-java 6.0.X serverTimezone 时区设置等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)