Java Default Timezone Issue Under Linux
2015-08-12 19:02
876 查看
1、Description
I encountered a problem with Java 1.7 running under Redhat Linux where Java was not detecting the correct default timezone from the operating system. It was defaulting to the
Under Linux, you can force Java to use a particular timezone by setting the
You can also force the use of a particular default timezone in Java either through programming or with a Java system property. The programming alternative is the least attractive as it requires that you change your Java programs by adding a line early in the program like this:
I encountered a problem with Java 1.7 running under Redhat Linux where Java was not detecting the correct default timezone from the operating system. It was defaulting to the
US/Pacifictimezone even though the Linux date command returned the correct timezone (
US/Easternin 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.2、Solution
Under Linux, you can force Java to use a particular timezone by setting the
TZenvironment variable to an appropriate value. To set the
TZenvironment variable for all instances of thebash shell, create a shell script in the
/etc/profile.ddirectory with a line similar to this:
export TZ="US/Eastern"[Please note: All of the examples on this page use the
US/Easterntimezone. Please substitute your timezone, as appropriate.]If your system does not support the
/etc/profile.ddirectory, add the previous line to the
/etc/profileinitialization script.Under most versions of Linux, you can determine your system default timezone by checking where the symbolic link
/etc/localtimepoints to by using the
ls -l /etc/localtimecommand. The response will look something like this:
/etc/localtime -> /usr/share/zoneinfo/US/EasternThe portion of the name after
/usr/share/zoneinfo/is your
TZvalue.Any shell scripts that are started through
/etc/init.ddo not run the
/etc/profilecode. Therefore, any Java programs you start via
/etc/init.dmust have an explicit
export TZ="US/Eastern"in order to work correctly.Once you have set the
TZvariable (you must log out and log back in to see the change), it will become part of your shell environment. To check that you have successfully set the value of
TZ, type the following command at your shell prompt:
echo $TZ3、Alternatives
You can also force the use of a particular default timezone in Java either through programming or with a Java system property. The programming alternative is the least attractive as it requires that you change your Java programs by adding a line early in the program like this:
TimeZone.setDefault(TimeZone.getTimeZone("US/Eastern"))To set the default timezone using a system property, set the
user.timezoneproperty to your time zone. One place to do this is on command line that starts your Java interpreter using the
-Dstartup option, like this:
java -Duser.timezone=US/Eastern CLASS_NAMEWhile this last technique works, be forewarned that this property is not part of the Java specification.
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统