如何在JAVA中使用joda时间将ISO8601转换为utc?

时间:2017-04-04 16:00:36

标签: java jodatime timezone-offset

我想转换:

2014-08-12T05:43:00-05:00 (YYYY-MM-DD"T"HH:MM:SS-OFFSET)

要:

20140812104300Z (YYYYMMDDHHMMSSZ)

1 个答案:

答案 0 :(得分:1)

终于明白了:

import java.text.ParseException;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

public class mydate {

    public static void main(String[] args) throws ParseException {
        // TODO Auto-generated method stub

        java.util.Date date = new DateTime("2014-08-12T05:43:00-05:00").toDate();
        DateTime dateTimeUtc = new DateTime( date, DateTimeZone.UTC ); // Joda-Time can convert from java.util.Date type which has no time zone.
        String output = dateTimeUtc.toString().replace("-", "").replace("T", "").replace(":", "").substring(0,14)+"Z"; // Defaults to ISO 8601 format.
        System.out.println(output);

    }

}

输入:2014-08-12T05:43:00-05:00

输出:20140812104300Z