如何使用 JAVA 中的 joda 时间将 ISO8601 转换为 utc?
How do I convert ISO8601 to utc using joda time in JAVA?
我要转换:
2014-08-12T05:43:00-05:00 (YYYY-MM-DD"T"HH:MM:SS-OFFSET)
收件人:
20140812104300Z (YYYYMMDDHHMMSSZ)
终于明白了:
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
我要转换:
2014-08-12T05:43:00-05:00 (YYYY-MM-DD"T"HH:MM:SS-OFFSET)
收件人:
20140812104300Z (YYYYMMDDHHMMSSZ)
终于明白了:
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