如何将java.util.Date转换为GMT格式

如何将java.util.Date转换为GMT格式,java,Java,我有一个字符串“2014-07-02T17:12:36.488-01:00”,显示山区时区。我将其解析为java.util.date格式。现在我需要将其转换为GMT格式。有人能帮我吗 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); Object dd = null; try { dd=sdf.parseObject("2014-07-02T17:12:36.488-0

我有一个字符串“2014-07-02T17:12:36.488-01:00”,显示山区时区。我将其解析为java.util.date格式。现在我需要将其转换为GMT格式。有人能帮我吗

  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    Object dd = null;
    try {
         dd=sdf.parseObject("2014-07-02T17:12:36.488-01:00");
        System.out.println(dd);
    } catch (ParseException e) {
        e.printStackTrace();`enter code here`
    }
    SimpleDateFormat gmtDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    gmtDateFormat.setTimeZone(java.util.TimeZone.getTimeZone("GMT"));
System.out.println("Current Date and Time in GMT time zone:+ gmtDateFormat.format(dd));

您的代码中存在一些问题。例如,格式字符串与正在分析的字符串的实际格式不匹配

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
Object dd = null;
try {
    dd = sdf.parse("2014-07-02T17:12:36.488-01:00");
    System.out.println(dd);
} catch (ParseException e) {
    e.printStackTrace();
}

SimpleDateFormat gmtDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssX");
gmtDateFormat.setTimeZone(java.util.TimeZone.getTimeZone("GMT"));

System.out.println("Current Date and Time in GMT time zone:" + gmtDateFormat.format(dd));
要以您喜欢的时区打印当前日期,请在
SimpleDateFormat
对象上设置要使用的时区。例如:

// Create a Date object set to the current date and time
Date now = new Date();

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("Current date and time in GMT: " + df.format(now));

df.setTimeZone(TimeZone.getTimeZone("IST"));
System.out.println("Current date and time in IST: " + df.format(now));

您的代码中存在一些问题。例如,格式字符串与正在分析的字符串的实际格式不匹配

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
Object dd = null;
try {
    dd = sdf.parse("2014-07-02T17:12:36.488-01:00");
    System.out.println(dd);
} catch (ParseException e) {
    e.printStackTrace();
}

SimpleDateFormat gmtDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssX");
gmtDateFormat.setTimeZone(java.util.TimeZone.getTimeZone("GMT"));

System.out.println("Current Date and Time in GMT time zone:" + gmtDateFormat.format(dd));
要以您喜欢的时区打印当前日期,请在
SimpleDateFormat
对象上设置要使用的时区。例如:

// Create a Date object set to the current date and time
Date now = new Date();

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("Current date and time in GMT: " + df.format(now));

df.setTimeZone(TimeZone.getTimeZone("IST"));
System.out.println("Current date and time in IST: " + df.format(now));

可能重复的可能重复的可能重复的感谢您的支持Wed Jul 02 23:42:36 IST 2014 GMT时区的当前日期和时间:2014-07-02 18:12:36Z..我得到这样的输出。Z是什么(2014-07-02 18:12:36Z)。同时IST MST时差是11.30 bt这里是12.30小时。我能做些什么来获得准确的值??Z是UTC/GMT时区的指示器。感谢您的支持Wed Jul 02 23:42:36 IST 2014 GMT时区中的当前日期和时间:2014-07-02 18:12:36Z..我得到了这样的输出。Z是什么(2014-07-02 18:12:36Z)。在同一时间,IST MST时差是11.30 bt,这里是12.30小时。如何获得准确值?Z是UTC/GMT时区的指示器。