为什么java.util.Date给我错误的时间? [英] Why java.util.Date is giving me wrong time?

查看:251
本文介绍了为什么java.util.Date给我错误的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.out.println(new Date());

Thu Feb 23 04:57:57 ACT 2012

我正在运行它在一个主要的方法。而我的系统当前时间是PKT。但它给我ACT时间。

I am running it in a main method. And my system current time is PKT. But it's giving me ACT time.

任何想法?如何获得正确的系统时间?

Any idea? How to get correct system's time?

推荐答案

问题出现在时区,而不是 Date 值本身。因此,不要打印当前日期,而是使用这样的东西来打印当前时区:

The problem appears to be with the time zone, not the Date value itself. So instead of printing out the current date, use something like this to print out the current time zone:

import java.util.TimeZone ;

public class Test {
    public static void main(String[] args) throws Exception {
        TimeZone zone = TimeZone.getDefault();
        System.out.println(zone.getDisplayName());
        System.out.println(zone.getID());
    }
}

例如,对于打印的我:

Greenwich Mean Time
Europe/London

一旦你知道Java认为你的默认时区的更多细节,你可以开始寻找为什么它认为的。有没有什么关于你如何运行非标准的Java?任何看起来像不正确时区的环境变量?

Once you know more details about what Java thinks your default time zone is, you can start looking for why it thinks that. Is there anything about how you're running Java which is non-standard? Any environment variables which look suspiciously like the incorrect time zone?

您可能还想打印出以下系统属性,用于确定时区: p>

You might also want to print out the following system properties, which are used when determining the time zone:

user.timezone
user.country
java.home

(打印出 获取默认时区 - user.timezone 被设置为获取它的一部分,如果没有设置。)

(Print them out before getting the default time zone - user.timezone is set as part of fetching it, if it wasn't set before.)

这篇关于为什么java.util.Date给我错误的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆