300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Wed Dec 20 00:00:00 GMT+0800 (中国标准时间) 时间转换

Wed Dec 20 00:00:00 GMT+0800 (中国标准时间) 时间转换

时间:2019-08-27 20:55:57

相关推荐

Wed Dec 20  00:00:00 GMT+0800 (中国标准时间) 时间转换

转换yyyy-MM-dd HH:mm:ss( DateUtils)

import java.text.SimpleDateFormat;import java.util.Date;import java.util.Locale;import java.util.regex.Pattern;/*** @ Author :L&C* @ Date : /12/31 15:13* @ Description :时间转换*/public class DateUtils {public final static String FORMAT_STRING = "yyyy-MM-dd HH:mm:ss";public final static String FORMAT_STRING2 = "EEE MMM dd yyyy HH:mm:ss z";public final static String[] REPLACE_STRING = new String[]{"GMT+0800", "GMT+08:00"};public final static String SPLIT_STRING = "(中国标准时间)";public static String parseTimeZone(String dateString) {try {dateString = dateString.split(Pattern.quote(SPLIT_STRING))[0].replace(REPLACE_STRING[0], REPLACE_STRING[1]);//转换为dateSimpleDateFormat sf1 = new SimpleDateFormat(FORMAT_STRING2, Locale.ENGLISH);Date date = sf1.parse(dateString);return new SimpleDateFormat(FORMAT_STRING).format(date);} catch (Exception e) {throw new RuntimeException("时间转化格式错误" + "[dateString=" + dateString + "]" + "[FORMAT_STRING=" + FORMAT_STRING + "]");}}public static void main(String[] args) {String date = parseTimeZone("Wed Dec 20 00:00:00 GMT+0800 (中国标准时间)");System.out.println(date);}}

Spring Boot中返回date为EEE MMM dd HH:mm:ss z yyyy(Wed Dec 20 00:00:00 CST ),转换为 yyyy-MM-dd HH:mm:ss

在实体类定义中,定义为date的字段上添加

@JsonFormat(timezone = “GMT+8”, pattern = “yyyy-MM-dd HH:mm:ss”)

并且在其get方法中也添加:

@JsonFormat(pattern = “yyyy-MM-dd HH:mm:ss”, timezone = “GMT+08:00”)

此时再次获得的该字段的日期格式为yyyy-MM-dd HH:mm:ss

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。