Java Calendar.getInstance() 更改 Linux 中的默认时区

Java Calendar.getInstance() changing the default timezone in Linux

请看我的代码如下:

SimpleDateFormat sd = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);  
sd.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(sd.format(calendar.getTime()));

此时我的 Windows 系统的默认时区是美国东部时间,而我 运行 这段代码是 (28/09/2016 12:27 PM) 并且当我 运行 系统中的这段代码我得到的输出如下 - 这是预期的(EDT 到 GMT 转换):

28/09/2016 04:00:00   

但是当我在服务器(Red Hat Enterprise Linux 服务器版本 5.11)上 运行 时,我得到的输出如下:

28/09/2016 00:00:00

当我运行下面的命令到Linuxshell

date +%Z

返回低于输出

EDT

所以,我无法理解为什么转换没有发生。另外,我有一段代码如下:

SimpleDateFormat sd = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Calendar calendar = Calendar.getInstance();
sd.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(sd.format(calendar.getTime()));

在同一个 Linux 服务器中返回以下输出(运行 它于 28/09/2016 12:36 PM),其预期输出已转换为 GMT

28/09/2016 16:36:46   

此代码是 运行WebLogic 12c 上 J2EE 应用程序的一部分。如果您有任何线索,请分享可能导致上述情况的原因。谢谢。

您依赖默认时区。指定两个时区,转换应该有效。

28/09/2016 12:27:00 -> 28/09/2016 16:27:00 +0000

这是一些测试代码。

package com.ggl.testing;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class TimeZoneConversion {

    public static void main(String[] args) {
        SimpleDateFormat sdInput = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        sdInput.setTimeZone(TimeZone.getTimeZone("US/Eastern"));
        SimpleDateFormat sdOutput = new SimpleDateFormat(
                "dd/MM/yyyy HH:mm:ss Z");
        sdOutput.setTimeZone(TimeZone.getTimeZone("GMT"));

        try {
            String dateString = "28/09/2016 12:27:00";
            Date inputDate = sdInput.parse(dateString);
            System.out
                    .println(dateString + " -> " + sdOutput.format(inputDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

}