1、UTC格式:2021-11-25T12:19:27.547Z
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class Miracle01 {
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
private static SimpleDateFormat sdfutc = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
private static SimpleDateFormat sdfutc1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
//2、UTC格式:2021-11-25T12:30:29.656+00:00
private static SimpleDateFormat sdfutc2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz");
//3、UTC格式:2021-11-25T12:33:02.000+00:00
private static SimpleDateFormat sdfutc3 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.000z");
//4、UTC格式:2021-11-25T12:45:39.119+0000
private static SimpleDateFormat sdfutc4 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
public static void main(String[] args) {
dateToUtc(new Date());
}
public static void dateToUtc(Date date) {
sdfutc.setTimeZone(TimeZone.getTimeZone("UTC"));// sdfutc.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("北京时间: " + sdf.format(date));
System.out.println("UTC时间: " + sdfutc.format(date));
sdfutc1.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println("UTC时间: " + sdfutc1.format(date));
sdfutc2.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
System.out.println("UTC时间: " + sdfutc2.format(date).replace("GMT", ""));
sdfutc3.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
System.out.println("UTC时间: " + sdfutc3.format(date).replace("GMT", ""));
sdfutc4.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println("UTC时间: " + sdfutc4.format(date));
}
}
运行结果如下:
北京时间: 2021-11-25 20:19:27.547
UTC时间: 2021-11-25T12:19:27.547Z
UTC时间: 2021-11-25T12:19:27.547Z
UTC时间: 2021-11-25T12:19:27.547+00:00
UTC时间: 2021-11-25T12:19:27.547+00:00
UTC时间: 2021-11-25T12:19:27.547+0000