日期:2014-05-16  浏览次数:20762 次

Java默认的Redhat Linux Fedora中的时区问题
Java默认的Redhat Linux Fedora中的时区问题

杰夫·莫特拉姆(尖塔点商务杰夫)写的。

放在2004年12月5日,笔者在公共领域。

此文件提供“是”,没有任何明示的保证或暗示,包括但不限于为特定目的和不侵权,适销性,适用性的保证。笔者在任何情况下,是法律责任的任何索赔,损害赔偿或其他责任,无论是在合同,侵权或其他行动,产生出或在本文档或本文档中使用或其他交易。

说明
我遇到了一个与Java 1.4.2的 ??运行在Fedora的Redhat Linux(核心版本1)Java是没有检测到从操作系统的正确的默认时区的问题。它被拖欠美国/太平洋时区,即使Linux的日期命令返回正确的时区(美国/东部在我的情况)。

来测试一下你的JVM的默认时区是,编译和运行以下代码:

import java.util.Date;
import java.util.TimeZone;

public class TimeTest {

    public static void main(String args[]) {
long time = System.currentTimeMillis();
String millis = Long.toString(time);
Date date = new Date(time);
System.out.println("Current time in milliseconds = " + millis + " => " + date.toString());
System.out.println("Current time zone: " + TimeZone.getDefault().getID());
    }
}
如果Java是使用默认时区不匹配您的系统时区(Linux的日期命令检查的方法之一),JVM没有成功检测系统时区。


Linux下的解决方案,您可以强制Java使用一个特定的时区设置TZ环境变量到一个适当的值。要设置TZ环境变量的所有实例的bash外壳,与此类似行的/ etc / profile.d目录中创建一个shell脚本:

出口雅轩=“美国/东部”
[ 请注意:此页面上的例子都使用美国/东部时区。请代替你的时区,适当的。]

如果您的系统不支持的/ etc / profile.d 目录,前行添加到/ etc / profile文件 的初始化脚本。

在大多数版本的Linux,你可以通过检查符号链接来确定您的系统默认时区 / etc / localtime的点使用的ls-l / etc / localtime的命令。响应将看起来像这样:

/ etc / localtime的 - >的/ usr /共享/ zoneinfo中/美/东区
部分名称之后的/ usr / share / zoneinfo中/ 雅轩价值。

通过启动任何shell脚本 / etc / init.d下不运行/ etc / profile中 的代码。因此,任何Java程序启动通过 的/ etc / init.d中必须有一个明确的出口TZ =“美国/东部”才能正常工作。

一旦你已经设置TZ变量(您必须注销并重新登录,看到的变化),它会成为您的shell环境的一部分。要检查您已成功设置的值 雅轩,在shell提示符下键入以下命令:

回声$雅轩
的替代品,
你也可以强制Java中的一个特定的默认时区,可以通过编程或使用Java系统属性。编程的另一种方法是最不具吸引力,因为它要求你改变你的Java程序加入行早在这样的程序:

TimeZone.setDefault(TimeZone.getTimeZone(美国/东“))
使用系统属性来设置默认的时区,设置中 user.timezone您的时区的财产。一个地方要做到这一点就开始使用三维启动选项,这样你的Java解释器的命令行:

Java-Duser.timezone的=美国/东欧CLASS_NAME的
虽然这最后的技术工作,应当注意这个属性是不是Java规范的一部分。




Java Default Timezone Issue Under Fedora Redhat Linux

Written by Geoff Mottram (geoff at minaret dot biz).

Placed in the public domain on December 5, 2004 by the author.

This document is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the author be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with this document or the use or other dealings in this document.

Description
I encountered a problem with Java 1.4.2 running under Fedora Redhat Linux (Core Release 1) where Java was not detecting the correct default timezone from the operating system. It was defaulting to the US/Pacific timezone even though the Linux date command returned the correct timezone (US/Eastern in my case).

To test what the default timezone of your JVM is, compile and run the following code:

import java.util.Date;
import java.util.TimeZone;

public class TimeTest {

    public static void main(String args[]) {
long time = System.currentTimeMillis();
String millis = Long.toString(time);
Date date = new Date(time);
System.out.println("Current time in milliseconds = " + millis + " => " + date.toString());
System.out.println("Current time zone: " + TimeZone.getDefault().getID());
    }
}
If the default timezone that Java is using does not match your system timezone (the Linux date command is one way of checking), the JVM is not successfully detecting your system timezone.

Solution
Under Linux, you can force Java to use a particular timezone by setting the TZ environment variable t