java将UTC时间转换成本地时间

来源:互联网 发布:dnf决战人工智能数字 编辑:程序博客网 时间:2024/05/16 18:41

使用java将UTC格式的时间字符串转换成本地时间易读的时间字符串:

 public static void main(String args[]) {        String UTC = "2017-11-09T23:16:03.562Z";        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");        System.out.println(TimeZone.getTimeZone("UTC"));        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));        Date UtcDate = null;        try {            UtcDate = sdf.parse(UTC);        } catch (Exception e) {            return;        }        System.out.println(UtcDate);        SimpleDateFormat localFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        System.out.println(TimeZone.getDefault());        localFormater.setTimeZone(TimeZone.getDefault());        String localTime = localFormater.format(UtcDate.getTime());        System.out.println(localTime);    }

运行效果如下:

sun.util.calendar.ZoneInfo[id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]Fri Nov 10 07:16:03 CST 2017sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null]2017-11-10 07:16:03
原创粉丝点击