小编tur*_*tia的帖子

Java 日历在 MS Windows 中为“美国/圣地亚哥”区域返回错误的时间

我有一个桌面应用程序(必须在 Windows 中运行),它需要在执行特定操作时记录时间戳,但是我在调​​用日历时获得的时间带有错误的时间和正确的时区。运行一些测试,我发现如果我取消选中 Windows 时区设置上的“自动调整夏令时时钟”复选框,日历会提供正确的小时。Linux 上不存在此问题。不幸的是,我不允许在 Windows 机器上进行任何更改。

更多信息:

  • Windows 时钟中设置的时区:“美国/圣地亚哥”(-4:00)。
  • 测试代码:

    public class Test2 {
    
        public static void main(String[] args) {
            SimpleDateFormat sdfBase = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
            long milisBase = Calendar.getInstance().getTimeInMillis();
            Calendar calenBase = Calendar.getInstance();
            Date fechaBase = calenBase.getTime();
            String fechaStringBase = sdfBase.format(fechaBase);
            TimeZone tzBase = Calendar.getInstance().getTimeZone();
    
    
            System.out.println("Time (in mill): " + milisBase);
            System.out.println("TimeZone: " + tzBase.getDisplayName() + " - " + tzBase.getID() + " -Offset: " + tzBase.getRawOffset());
            System.out.println("Date Calendar: " + fechaBase);
            System.out.println("Fecha StringBase: " + fechaStringBase); …
    Run Code Online (Sandbox Code Playgroud)

java time timezone calendar date

3
推荐指数
1
解决办法
1431
查看次数

标签 统计

calendar ×1

date ×1

java ×1

time ×1

timezone ×1