`
顽石OK07
  • 浏览: 80970 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

JDK timezone 设置

阅读更多

转载:http://www.shaojiahao.org/java/jdk-timezone-setting

 

 

最近安装了win7,发现公司某些基于JDK1.4的项目在本地跑有些许问题,其中之一就是用Calendar获得的时间问题。通过多次实验,发现是win7使用UTC导致的问题,但是JDK1.5没有这些问题,估计是1.4的小bug吧,嘿嘿~在我本机,1.4获得的时间慢了8小时,其实它获得的时间实际上也没有问题,为什么这么说呢,因为他显示是如Mon Oct 25 03:00:29 GMT 2010,而GMT是英国格林威治时间,所以比现在慢8小时也是正确的,注意,我们是GMT+08:00!

这个时间虽然没有问题,但是当我们用SimpleDateFormat的时候,默认format出来的时间也就是慢8小时了,在我们的界面显示出来就悲剧了~我们可以采用TimeZone来解决此问题,演示代码如下:

package com.test;

import java.util.*;

public class Test{
public static void main(String args[]){
TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
Calendar c = Calendar.getInstance();
Date d = c.getTime();
System.out.println(d);
}
}

看到了吗,就是TimeZone.setDefault(TimeZone.getTimeZone(“GMT+8″))这一句,只要这么一设置,就全局有效了,然后取得的时间就是正确的本地时间了,如Mon Oct 25 11:00:29 GMT+08:00 2010~

当然,你也可以选择使用jvm启动参数-Duser.timezone来设置,效果一样,不过如果你的程序跑在容器上的话最好就是用程序来设置了~

分享到:
评论

相关推荐

    tzdb.dat 高版本jdk15 14 11 缺少 timezone calendar

    java.io.FileNotFoundException 报错解决 高版本jdk 运行 java -jar 时 缺少 jdk1.8中的这个文件

    Oracle sqldeveloper without jdk (win+linux)

    To switch the time zone used for formatting, change the client O/S time zone setting and restart SQL Developer. 4.4 Known Globalization Issues This version of SQL Developer has the following ...

    springboot+mybatis+内置tomcat示例.rar

    serverTimezone=UTC #spring.datasource.username =root #spring.datasource.password =123456 #spring.datasource.driver-class-name =com.mysql.cj.jdbc.Driver #数据库配置 spring.datasource.url=jdbc:...

    jdbc-postgres

    OO 和 SQL 审查 在本课中,我们将复习 主键、外键 DDL(创建表,更改表) SQL 插 ... moment TIMESTAMP WITHOUT TIME ZONE , status int4, primary key (id) ); create table tb_order_product

    OM-Dtask:OpenMediation数据任务

    OpenMediation数据任务 OpenMediation数据任务是一个数据处理中心。 该服务应运行单例。 它为提供api和pb数据 用法 包装 您可以使用打包。... -Duser.timezone=UTC \ -Xmx3g \ -Xms1g \ -server " RUN_ARGS=

    基于SSM实现的高考平行志愿模拟录取系统源码+项目说明(数据库课程设计).zip

    【资源说明】 1、项目源码在上传前,都经过本地成功运行,功能测试ok。...useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai ``` 4. 启动项目, 访问浏览器访问 localhost:8080/index.html

    新版Android开发教程.rar

    o JDK 5 or JDK 6 (JRE alone is not sufficient) o Apache Ant 1.6.5 or later for Linux and Mac, 1.7 or later for Windows o Not Not Not Not compatible with Gnu Compiler for Java (gcj) Note: Note: Note: ...

    Java开发实战1200例(第1卷).(清华出版.李钟尉.陈丹丹).part3

    9.2 SimpleDateFormat与TimeZone 类的使用 250 实例196 日期格式有效性判断 250 实例197 常见日期格式使用 252 实例198 查看本地时区 253 实例199 简单的时区转换工具 254 9.3 System类的使用 255 实例200 查看常用...

    疯狂JAVA讲义

    9.4.3 TimeZone类 337 9.5 正则表达式 338 9.5.1 创建正则表达式 338 9.5.2 使用正则表达式 341 9.6 程序国际化 345 9.6.1 Java国际化的思路 346 9.6.2 Java支持的语言和国家 346 9.6.3 完成程序国际化 347 ...

    spring-framework-reference4.1.4

    Not Using Commons Logging ................................................................... 12 Using SLF4J ..............................................................................................

    spring-framework-reference-4.1.2

    Not Using Commons Logging ................................................................... 12 Using SLF4J ..............................................................................................

Global site tag (gtag.js) - Google Analytics