• java 统一处理大部分时间字符串为Date格式


    public static void main(String[] args) {
            String a = "2016-11-30 17:16:00";
            String b = "2015-07-13 11:11:22.02";
            String c = "1394438072";
            String d = "Jul 26, 2017 2:27:13 PM";
            String e = "2019-03-31";
            String g = "2019-04-01 03:13";
            String h = "1584516482676";
            String i = "2014-06-27T08:11:07.168Z";
            String j = "Tue Jan 14 16:11:26 +0800 2020";
    
            DateFormat formatTo = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = formatDate(a);
            Date date2 = formatDate(b);
            Date date3 = formatDate(c);
            Date date4 = formatDate(d);
            Date date5 = formatDate(e);
            Date date6 = formatDate(g);
            Date date7 = formatDate(h);
            Date date8 = formatDate(i);
            System.out.println(date!=null?formatTo.format(date):null);
            System.out.println(date2!=null?formatTo.format(date2):null);
            System.out.println(date3!=null?formatTo.format(date3):null);
            System.out.println(date4!=null?formatTo.format(date4):null);
            System.out.println(date5!=null?formatTo.format(date5):null);
            System.out.println(date6!=null?formatTo.format(date6):null);
            System.out.println(date7!=null?formatTo.format(date7):null);
            System.out.println(date8!=null?formatTo.format(date8):null);
            System.out.println();
        }
    
        private static Date formatDate(String str) {
            SimpleDateFormat resultSdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            SimpleDateFormat resultSdfDate = new SimpleDateFormat("yyyy-MM-dd");
            if (str == null) {
                return null;
            }
            if (str.contains("CST")) {
                try {
                    Date date = resultSdf.parse(str);
                    return date;
                } catch (ParseException e) {
                    log.error("时间转换失败:{}", e.getMessage());
                }
            } else if (str.contains("Z")) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'.'sss'Z'");
                try {
                    Date date = sdf.parse(str);
                    return date;
                } catch (ParseException e) {
                    log.error("时间转换失败:{}", e.getMessage());
                }
    
            } else if (str.contains("-") && str.contains(":") && str.length() == 16) {
                try {
                    Date newDate = resultSdf.parse(str + ":00");
                    return newDate;
                } catch (ParseException e) {
                    log.error("时间转换失败:{}", e.getMessage());
                }
            } else if (str.contains("-") && str.contains(":")) {
                try {
                    Date newDate = resultSdf.parse(str);
                    return newDate;
                } catch (ParseException e) {
                    log.error("时间转换失败:{}", e.getMessage());
                }
            } else if (str.contains("-") && !str.contains(":")) {
                try {
                    Date newDate = resultSdfDate.parse(str);
                    return newDate;
                } catch (ParseException e) {
                    log.error("时间转换失败:{}", e.getMessage());
                }
            } else if (str.contains(",") && (str.contains("AM") || str.contains("PM"))) {
                try {
                    DateFormat formatFrom = new SimpleDateFormat("MMM dd,yyyy KK:mm:ss aa", Locale.ENGLISH);
                    Date date = formatFrom.parse(str);
                    return date;
                } catch (ParseException e) {
                    log.error("时间转换失败:{}", e.getMessage());
                }
            } else {
                if (str.length() != 13) {
                    int decrease = 13 - str.length();
                    for (int i = 0; i < decrease; i++) {
                        str += "0";
                    }
                }
                try {
                    Date date = new Date(Long.parseLong(str));
                    return date;
                } catch (NumberFormatException e) {
                    log.error("时间转换失败:{}", e.getMessage());
                }
            }
            try {
                Date date = new Date(str);
                return date;
            } catch (NumberFormatException e) {
                log.info("时间转换失败:{}", e.getMessage());
            }
            return null;
        }
    

      

    借鉴于该博客:https://blog.csdn.net/zzq900503/article/details/38925225

    可以处理以下格式:

        String a = "2016-11-30 17:16:00";
        String b = "2015-07-13 11:11:22.02";
        String c = "1394438072";
        String d = "Jul 26, 2017 2:27:13 PM";
        String e = "2019-03-31";
        String g = "2019-04-01 03:13";
        String h = "1584516482676";
        String i = "2014-06-27T08:11:07.168Z";
        String j = "Tue Jan 14 16:11:26 +0800 2020";

     

  • 相关阅读:
    2019-1-17 水晶报表自动补空行及格线(无分组版)
    通过ssh证书远程登录
    kali linux下不能以root权限运行vlc的解决办法
    SSH服务:packet_write_wait: Connection to 67.218.143.160 port 22: Broken pipe错误处理
    python系列--函数--递归函数
    python虚拟环境安装pyqt5
    docker API接口service update错误记录 error while removing network:…
    docker service create api参数
    docker api参数文档
    docker 集群
  • 原文地址:https://www.cnblogs.com/zhizhu1120418975/p/12518228.html
Copyright © 2020-2023  润新知