将日期转换为GMT日期格式

时间:2014-01-15 18:28:50

标签: java

请帮助将格式为“ 2007年5月16日上午12:00:00 ”的格式转换为GMT数据格式,如 java中的“ 2007-05-16T12:00:00.000 + 0000 ” 使用以下方法

public  String getGMTMillis(Date time)
{
    if (time != null)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.setTimeZone(TimeZone.getTimeZone("GMT"));

        return "" + calendar.getTimeInMillis();
    }

    return "";
}

2 个答案:

答案 0 :(得分:1)

尝试:

public String getGmtMillis(Date time)
{
    if (time == null)
    {
        return "";
    }
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZZ");
    df.setTimeZone(TimeZone.getTimeZone("GMT"));
    return df.format(time);
}

答案 1 :(得分:0)

我相信您要求ISO 8601标准的日期时间格式。

Joda-Time 2.3库使用该标准格式作为toString方法的默认格式。

String dateTimeString = new DateTime( DateTimeZone.UTC );

输出类似于:

2014-01-18T12:34.123Z