java Tue Aug 21 2018 00:00:00 GMT+0800 (中国标准时间) 00:00:00 时间格式转换

package com.meizu.mdsp.web.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Pattern;

/**
 * @author liangzhicheng
 * @version V1.0
 */
public class DateUtils {

    public final static String FORMAT_STRING = "yyyy-MM-dd HH:mm:ss";

    public final static String[] REPLACE_STRING = new String[]{"GMT+0800", "GMT+08:00"};

    public final static String SPLIT_STRING = "(中国标准时间)";

    public static Date str2Date(String dateString) {
        try {
            dateString = dateString.split(Pattern.quote(SPLIT_STRING))[0].replace(REPLACE_STRING[0], REPLACE_STRING[1]);
            SimpleDateFormat sf1 = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss z", Locale.US);
            Date date = sf1.parse(dateString);
            return date;
        } catch (Exception e) {
            throw new RuntimeException("时间转化格式错误" + "[dateString=" + dateString + "]" + "[FORMAT_STRING=" + FORMAT_STRING + "]");
        }
    }

    public static void main(String[] args) {
        str2Date("Tue Aug 21 2018 00:00:00 GMT+0800 (中国标准时间) 00:00:00");
    }
}

关注我的技术公众号《漫谈人工智能》,每天推送优质文章

java Tue Aug 21 2018 00:00:00 GMT+0800 (中国标准时间) 00:00:00 时间格式转换_第1张图片

你可能感兴趣的:(java)