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'
"
);
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));
运行结果如下:
北京时间: 2021-11-25 20:19:27.547
UTC时间: 2021-11-25T12:19:27.547Z
2)方式2
代码
实现
如下:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
* @author Miracle Luna
* @date 2021/11/25
public class Miracle02 {
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.SSSX");
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));
运行结果如下:
北京时间: 2021-11-25 20:26:20.153
UTC时间: 2021-11-25T12:26:20.153Z
2、UTC格式:2021-11-25T12:30:29.656+00:00
代码实现如下:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
* @author Miracle Luna
* @date 2021/11/25
public class Miracle03 {
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.SSSz");
public static void main(String[] args) {
dateToUtc(new Date());
public static void dateToUtc(Date date) {
sdfutc.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
System.out.println("北京时间: " + sdf.format(date));
System.out.println("UTC时间: " + sdfutc.format(date).replace("GMT", ""));
运行结果如下:
北京时间: 2021-11-25 20:30:29.656
UTC时间: 2021-11-25T12:30:29.656+00:00
3、UTC格式:2021-11-25T12:33:02.000+00:00
代码实现如下:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
* @author Miracle Luna
* @date 2021/11/25
public class Miracle04 {
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static SimpleDateFormat sdfutc = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.000z");
public static void main(String[] args) {
dateToUtc(new Date());
public static void dateToUtc(Date date) {
sdfutc.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
System.out.println("北京时间: " + sdf.format(date));
System.out.println("UTC时间: " + sdfutc.format(date).replace("GMT", ""));
运行结果如下:
北京时间: 2021-11-25 20:33:02
UTC时间: 2021-11-25T12:33:02.000+00:00
4、UTC格式:2021-11-25T12:45:39.119+0000
代码实现如下:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
* @author Miracle Luna
* @date 2021/11/25
public class Miracle05 {
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static SimpleDateFormat sdfutc = 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));
运行结果如下:
北京时间: 2021-11-25 20:47:12
UTC时间: 2021-11-25T12:47:12.674+0000