【问题标题】:how to set default time zone to a different time zone than that of your system and store the date如何将默认时区设置为与系统不同的时区并存储日期
【发布时间】:2016-06-14 19:43:45
【问题描述】:

我在美国中部时间,并试图在 EST 中存储低于日期,但它给了我一个错误的输出:

字符串reportedDate = "06-14-2016 02:10:59 PM";

下面是我的输出:

时区:东部标准时间
地区:美国/纽约
日期:06-14-2016 03:10:59 PM

我使用了以下代码:

SimpleDateFormat sdfInCentral = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss a");   
String reportedDate = "06-14-2016 02:10:59 PM";    
Date birthDate = sdfInCentral.parse(reportedDate);   
TimeZone.setDefault(TimeZone.getTimeZone(ZoneId.of("America/New_York")));   
TimeZone tz = TimeZone.getDefault();   
sdfInCentral.setTimeZone(tz);     

【问题讨论】:

    标签: java datetime timezone


    【解决方案1】:

    根据上述信息,您可以尝试这样的事情

        SimpleDateFormat sdfInCentral= new SimpleDateFormat("MM-dd-yyyy hh:mm:ss a");
        sdfInCentral.setTimeZone(TimeZone.getTimeZone("EST"));
        String reportedDate = "06-14-2016 02:10:59 PM";    
        Date date = sdfInCentral.parse(reportedDate);
        System.out.println(date); 
    

    【讨论】:

    • 我不明白这如何回答原始问题。此外,您应该使用America/New_York,而不是EST。夏令时在该日期有效,将时区设置为EST 不会反映这一点。见this question
    • +100 致马特约翰逊,切勿使用三个字母的时区缩写
    猜你喜欢
    • 2019-06-14
    • 2020-07-08
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多