Java之把格林威治时间格式转换成北京时间格式 Posted on 2020-03-23 | Post modified: 2020-03-23 | In 时间转换 | Visitors: | View Java之格林威治时间格式转换成北京时间格式摘要:日常工作中,我们经常会遇到这样的需要,默认的java获取的时间是格林威治格式的,需要转换成正常格式,那么可以通过下面的方式实现,代码如下: 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364/** * Copyright 2020. yaodouwang.com Studio All Right Reserved * <p> * Create on 03-23 12:45 * Created by zhaoxinguo * Version 2.0.0 */package com.mtons.mblog;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.TimeZone;/** * @description: Java之格林威治时间格式转换成北京时间格式 * @author zhaoxinguo * @date 2020/3/23 12:45 */public class DateUtils { /** * 支持jdk1.6的写法<br/> * 解析2015-12-27T14:20:34+08:00格式类型的时间<br/> * 将2015-12-27T14:20:34+08:00转换成2015-12-27 14:20:34<br/> * @param str * @return * @throws Exception */ public static String getTimestampTimeV16(String str) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai"); sdf.setTimeZone(tz); Date date = sdf.parse(str); String string = date.toString(); return string; } /** * 支持jdk1.7的写法<br/> * 解析2015-12-27T14:20:34+08:00格式类型的时间<br/> * 将2015-12-27T14:20:34+08:00转换成2015-12-27 14:20:34<br/> * @param str * @return * @throws Exception */ public static String getTimestampTimeV17(String str) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX"); Date date = sdf.parse(str); String string = date.toString(); return string; } /** * 将1464166640(当前时间的秒)转换成:yyyy-MM-dd HH:mm:ss格式 * @param str * @return */ public static String getTimeToDay(String str){ Date date = new Date(1464166640L*1000); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); return format.format(date); }} 扫一扫,关注微信订阅号 坚持原创技术分享,您的支持将鼓励我继续创作! Donate WeChat Pay Alipay Post author: MiCai Post link: https://micai-code.github.io/2020/03/23/Java之把格林威治时间格式转换成北京时间格式/ Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.