package ins.platform.utils; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.apache.log4j.Logger; public class TimeUtil { private static Logger logger = Logger.getLogger(TimeUtil.class); private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";//3 private static final String DATE_FORMAT_DATE = "yyyyMMdd";//12 private static final String DATE_FORMAT2 = "yyyyMMddHHmmss";//这个方法不要放入dateFormats 他会处理类似于2019-04-04 00格式数据,并处理的时间不对 private static final String DATE_FORMAT3 = "yyyy-MM-dd HH:mm:ss.SSS z";//1 private static final String DATE_FORMAT4 = "yyyyMMdd HHmmss";//4 private static final String DATE_FORMAT5 = "yyyy-MM-dd HHmmss";//5 private static final String DATE_FORMAT7 = "yyyy-MM-dd HH-mm-ss";//6 private static final String DATE_FORMAT8 = "yyyy-MM-dd HH:mm";//8 private static final String DATE_FORMAT9 = "yyyy-MM-dd HH-mm";//9 private static final String DATE_FORMAT10 = "yyyy-MM-dd HHmm";//7 private static final String DATE_FORMAT11 = "yyyy-MM-dd HH";//10 private static final String DATE_FORMAT12 = "yyyy-MM";//13 private static final String DATE_FORMAT_MILLISECOND = "yyyy-MM-dd HH:mm:ss.SSS";//2 private static final String DATE_YEAR = "yyyy";//15 private static final String DATE_MONTH = "MM";//16 private static final String DATE_DAY = "dd";//17 private static final String DATE_TIME = "HH:mm:ss";//14 private static final String DATE_DATE = "yyyy-MM-dd";//11 private static final String DATE_DATE_D = "yyyyMMdd"; static List<DateFormat> dateFormats = null; static { dateFormats = new ArrayList<>(); //按照时间格式从复杂维度到简单维度放入 --如果有特殊格式,只需要添加就可以了 dateFormats.add(new SimpleDateFormat(DATE_FORMAT3)); dateFormats.add(new SimpleDateFormat(DATE_FORMAT_MILLISECOND)); dateFormats.add(new SimpleDateFormat(DATE_FORMAT)); dateFormats.add(new SimpleDateFormat(DATE_FORMAT4)); dateFormats.add(new SimpleDateFormat(DATE_FORMAT5)); dateFormats.add(new SimpleDateFormat(DATE_FORMAT7)); dateFormats.add(new SimpleDateFormat(DATE_FORMAT10)); dateFormats.add(new SimpleDateFormat(DATE_FORMAT8)); dateFormats.add(new SimpleDateFormat(DATE_FORMAT9)); dateFormats.add(new SimpleDateFormat(DATE_FORMAT11)); dateFormats.add(new SimpleDateFormat(DATE_DATE)); dateFormats.add(new SimpleDateFormat(DATE_FORMAT_MILLISECOND)); dateFormats.add(new SimpleDateFormat(DATE_FORMAT12)); dateFormats.add(new SimpleDateFormat(DATE_TIME)); dateFormats.add(new SimpleDateFormat(DATE_YEAR)); dateFormats.add(new SimpleDateFormat(DATE_MONTH)); dateFormats.add(new SimpleDateFormat(DATE_DAY)); } //字符串转日期格式 public static Date formatStr(String dateStr) { SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_MILLISECOND); Date date = null; try { date = sdf.parse(dateStr); } catch (ParseException e) { sdf = new SimpleDateFormat(DATE_FORMAT); try { date = sdf.parse(dateStr); } catch (ParseException e1) { sdf = new SimpleDateFormat(DATE_FORMAT8); try { date = sdf.parse(dateStr); } catch (ParseException e2) { sdf = new SimpleDateFormat(DATE_FORMAT11); try { date = sdf.parse(dateStr); } catch (ParseException e3) { sdf = new SimpleDateFormat(DATE_DATE); try { date = sdf.parse(dateStr); } catch (ParseException e4) { logger.error("日期转换异常",e4); } } } } } return date; } public enum TimeEnum { // yyyy-MM-dd HH:mm:ss YEAR("yyyy", "年份"), MM("yyyy-MM", "年月"), dd("yyyy-MM-dd", "年月日"), HH( "yyyy-MM-dd HH", "年月日 时"), mm("yyyy-MM-dd HH:mm", "年月日 时分"),ss("yyyy-MM-dd HH:mm:ss", "年月日 时分秒"); private String code; private String name; TimeEnum(String code,String name) { this.name = name; this.code = code; } public String getName() { return name; } public String getCode() { return code; } } /** * * @param dateStr 需要转换的时间字符串 * @param timeEnum 时间格式的枚举 * @return * @throws ParseException */ public static Date formatStrToDate(String dateStr,TimeEnum timeEnum) throws ParseException { Date date = null; if(dateStr == null || "".equals(dateStr) || "".equals(dateStr.trim())){ return null; } if(timeEnum !=null){ //按照定制的初始化 SimpleDateFormat sdf = new SimpleDateFormat(timeEnum.code); date = sdf.parse(dateStr); }else{ String[] dateArr = dateStr.split(" "); if(dateArr == null){ return null; } String pattern = ""; if(dateArr.length == 1){ //yyyy-MM-dd String str = dateArr[0]; if(str.length() == 4){ pattern = TimeEnum.YEAR.code; } if(str.length() == 7){ pattern = TimeEnum.MM.code; } if(str.length() == 10){ pattern = TimeEnum.dd.code; } }else if(dateArr.length == 2){ //yyyy-MM-dd //HH:mm:ss String str = dateArr[1]; if(str.length() == 2){ pattern = TimeEnum.HH.code; } if(str.length() == 5){ pattern = TimeEnum.mm.code; } if(str.length() == 8){ pattern = TimeEnum.ss.code; } } if(!"".equals(pattern)){ SimpleDateFormat sdf = new SimpleDateFormat(pattern); date = sdf.parse(dateStr); } } return date; } }