当前位置: 移动技术网 > IT编程>开发语言>Java > java正则实现各种日期格式化

java正则实现各种日期格式化

2019年07月22日  | 移动技术网IT编程  | 我要评论

java正则实现各种日期格式化

package com.st.test;
 
import java.text.dateformat;
import java.text.simpledateformat;
import java.util.date;
import java.util.hashmap;
import java.util.regex.pattern;
 
public class dateformatutil {
   
  @suppresswarnings("finally")
  public static string formatdate(string datestr){
 
    hashmap<string, string> dateregformat = new hashmap<string, string>();
    dateregformat.put(
        "^\\d{4}\\d+\\d{1,2}\\d+\\d{1,2}\\d+\\d{1,2}\\d+\\d{1,2}\\d+\\d{1,2}\\d*$",
        "yyyy-mm-dd-hh-mm-ss");//2014年3月12日 13时5分34秒,2014-03-12 12:05:34,2014/3/12 12:5:34
    dateregformat.put("^\\d{4}\\d+\\d{2}\\d+\\d{2}\\d+\\d{2}\\d+\\d{2}$",
        "yyyy-mm-dd-hh-mm");//2014-03-12 12:05
    dateregformat.put("^\\d{4}\\d+\\d{2}\\d+\\d{2}\\d+\\d{2}$",
        "yyyy-mm-dd-hh");//2014-03-12 12
    dateregformat.put("^\\d{4}\\d+\\d{2}\\d+\\d{2}$", "yyyy-mm-dd");//2014-03-12
    dateregformat.put("^\\d{4}\\d+\\d{2}$", "yyyy-mm");//2014-03
    dateregformat.put("^\\d{4}$", "yyyy");//2014
    dateregformat.put("^\\d{14}$", "yyyymmddhhmmss");//20140312120534
    dateregformat.put("^\\d{12}$", "yyyymmddhhmm");//201403121205
    dateregformat.put("^\\d{10}$", "yyyymmddhh");//2014031212
    dateregformat.put("^\\d{8}$", "yyyymmdd");//20140312
    dateregformat.put("^\\d{6}$", "yyyymm");//201403
    dateregformat.put("^\\d{2}\\s*:\\s*\\d{2}\\s*:\\s*\\d{2}$",
        "yyyy-mm-dd-hh-mm-ss");//13:05:34 拼接当前日期
    dateregformat.put("^\\d{2}\\s*:\\s*\\d{2}$", "yyyy-mm-dd-hh-mm");//13:05 拼接当前日期
    dateregformat.put("^\\d{2}\\d+\\d{1,2}\\d+\\d{1,2}$", "yy-mm-dd");//14.10.18(年.月.日)
    dateregformat.put("^\\d{1,2}\\d+\\d{1,2}$", "yyyy-dd-mm");//30.12(日.月) 拼接当前年份
    dateregformat.put("^\\d{1,2}\\d+\\d{1,2}\\d+\\d{4}$", "dd-mm-yyyy");//12.21.2013(日.月.年)
 
    string curdate =new simpledateformat("yyyy-mm-dd").format(new date());
    dateformat formatter1 =new simpledateformat("yyyy-mm-dd hh:mm:ss");
    dateformat formatter2;
    string datereplace;
    string strsuccess="";
    try {
      for (string key : dateregformat.keyset()) {
        if (pattern.compile(key).matcher(datestr).matches()) {
          formatter2 = new simpledateformat(dateregformat.get(key));
          if (key.equals("^\\d{2}\\s*:\\s*\\d{2}\\s*:\\s*\\d{2}$")
              || key.equals("^\\d{2}\\s*:\\s*\\d{2}$")) {//13:05:34 或 13:05 拼接当前日期
            datestr = curdate + "-" + datestr;
          } else if (key.equals("^\\d{1,2}\\d+\\d{1,2}$")) {//21.1 (日.月) 拼接当前年份
            datestr = curdate.substring(0, 4) + "-" + datestr;
          }
          datereplace = datestr.replaceall("\\d+", "-");
          // system.out.println(dateregexparr[i]);
          strsuccess = formatter1.format(formatter2.parse(datereplace));
          break;
        }
      }
    } catch (exception e) {
      system.err.println("-----------------日期格式无效:"+datestr);
      throw new exception( "日期格式无效");
    } finally {
      return strsuccess;
    }
  }
  public static void main(string[] args) {
    string[] datestrarray=new string[]{
        "2014-03-12 12:05:34",
        "2014-03-12 12:05",
        "2014-03-12 12",
        "2014-03-12",
        "2014-03",
        "2014",
        "20140312120534",
        "2014/03/12 12:05:34",
        "2014/3/12 12:5:34",
        "2014年3月12日 13时5分34秒",
        "201403121205",
        "1234567890",
        "20140312",
        "201403",
        "2000 13 33 13 13 13",
        "30.12.2013",
        "12.21.2013",
        "21.1",
        "13:05:34",
        "12:05",
        "14.1.8",
        "14.10.18" 
    };
    for(int i=0;i<datestrarray.length;i++){
      system.out.println(datestrarray[i] +"------------------------------".substring(1,30-datestrarray[i].length())+ formatdate(datestrarray[i]));
    }
     
  }
 
}

以上所述就是本文的全部内容了,希望大家能够喜欢。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网