Java时区bug,TimeZone.setDefault()只在当前线程有效
项目是基于GMT时间的,在系统启动的时候,我们就会调用TimeZone.setDefault(timeZone)将默认时区设为GMT。
后来突然发现,有时用户选择的时间经过后台一圈后回产生8个小时误差。又是间歇性的,要他重现的时候又偏不来。苦心debug,终于发现在部分线程中,时区还是GMT+8,后台某个调用可能把时区变为了GMT,8小时误差就产生了。网上一搜,原来是bug:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6352812
只在部分电脑上有问题,幸好production上是好的,写了一个程序来测试:
import java.util.Date;
import java.util.TimeZone;
public class Test {
@SuppressWarnings("unused")
public static void main(String[] args) throws InterruptedException {
DateThread thread = new DateThread();
thread.start();
TimeZone gmt = TimeZone.getTimeZone("GMT");
Date now = new Date();
System.out.println("main thread,before set timezone:" + now.toString());
TimeZone.setDefault(gmt);
Thread.currentThread().sleep(2000);
thread.resume();
System.out.println("main thread,after set timezone:" + now.toString());
}
}
class DateThread extends Thread {
@SuppressWarnings( { "unused", "deprecation" })
public void run() {
Date now = new Date();
System.out.println("sub thread,before set timezone:" + now.toString());
this.suspend();
System.out.println("sub thread,after set timezone:" + now.toString());
}
}
如果子线程输出的时区一样,就说明有bug
相关文档:
在Firefox 3.6中,当焦点在flash或者java applet对象上时,会出现一个环绕对象的虚线框,当将flash与java applet对象的大小设成100%时,点击对象后页面会出现滚动条,影响显示效果,解决的方法是使用如下的css定义:
:focus {
outline: 0;
}
这样在各个不同的浏览器中显示的效果都能保持一致。 ......
1、commons-math/commons-lang-math
以上两个包是apache下的,前者比后者的功能强大,后者有的功能前都有,后者主要解决平时程序中的一些基本的数学计算,主要是范围判断(*Range),随机数生成(JVMRandom,RandomUtils),分数处理(Fraction),数字转化、大小判断(NumberUtils)等。前者可以处理更复杂的数据分 ......
写一个Application程序,界面上放置两个Textfield,一个Button,用户在第一个Textfield中输入姓名后敲回车键可以在第二个Textfield中输出:“Welcome you,用姓名”。用户点击Button可以退出程序。
帮帮忙吧!简单的Java语言。谢谢啦! ......
服务器端:
package com.huahua;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerThread extends Thread {
......
Java编写一个函数交换两个变量的值
Java函数在传递过程中只能够传值,不能传址。这样,函数的参数在函数内部做任何变化就都不会反映到外部调用者来。所以解决之道就是要找到要交换对象的引用。对于普通的值类型,像int或者double这样的可以改传他们的包装类Integer和Double。而对于本来就是引用类型的对象,则需要对他们再 ......