Date与UTC时间相互转换-java

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值