任意字符串转时间戳,万能工具类
代码:
package cn.cncommdata.form.util; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * @author xxxx * @date: 2019/6/21 14:21 * @description */ @Component public class DateUtils { /** * 13为时间戳正则表达式 */ private static final String PATTERN_TIME_STAMP = "[0-9]{13}"; /** * 横杠时间正则表达式 */ private static final String PATTERN_ROD = "(1|2)[0-9]{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])"; /** * 斜杠时间正则表达式 */ private static final String PATTERN_SLASH = "(1|2)[0-9]{3}/(0[1-9]|1[0-2])/(0[1-9]|[1-2][0-9]|3[0-1])"; /** * 汉字年月日时间正则表达式 */ private static final String PATTERN_CHINESE_CHAR = "(1|2)[0-9]{3}年(0[1-9]|1[0-2])月(0[1-9]|[1-2][0-9]|3[0-1]日)"; /** * 获取当前timeMillis * * @return 当前时间戳 */ public Long getCurrentTimeMill() { Calendar calendar = Calendar.getInstance(); return calendar.getTimeInMillis(); } /** * 将不同类型的日期转化为时间戳,传到后台,万能方法; * * @param dateStr 字符串初始值,可以是yyyy/MM/dd,yyyy年MM月dd日,yyyy-MM-dd, * 还可以在之后添加时分秒后缀; * @return 13位时间戳的字符串 */ public String getDateAndFormat(String dateStr) { if (dateStr.matches(PATTERN_TIME_STAMP)) { return dateStr; } else if (dateStr.matches(PATTERN_ROD) || dateStr.matches(PATTERN_SLASH) || dateStr.matches(PATTERN_CHINESE_CHAR)) { Map map = new HashMap(); if (dateStr.matches(PATTERN_CHINESE_CHAR)) { map = chineseCharTime(dateStr); } else if (dateStr.matches(PATTERN_ROD)) { map = rodTime(dateStr, "-"); } else if (dateStr.matches(PATTERN_SLASH)) { map = rodTime(dateStr, "/"); } if ("no".equals(map.get("operate").toString())) { return "日期格式不匹配,禁止修改"; } return transMapToTimeStamp(map); } else { return "日期格式不匹配,禁止修改"; } } /** * 解析map格式的时间为 时间戳 * * @param map map格式的时间 * @return 时间戳字符串 */ private String transMapToTimeStamp(Map map) { StringBuilder builder = new StringBuilder(); if (map.get("hour") == null) { map.put("hour", "00"); } if (map.get("minute") == null) { map.put("minute", "00"); } if (map.get("second") == null) { map.put("second", "00"); } builder.append(map.get("year").toString()); builder.append("/"); builder.append(map.get("month").toString()); builder.append("/"); builder.append(map.get("day").toString()); builder.append(" "); builder.append(map.get("hour").toString()); builder.append("/"); builder.append(map.get("minute").toString()); builder.append("/"); builder.append(map.get("second").toString()); Date date = new Date(); Long timeStemp = 0L; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); try { date = simpleDateFormat.parse(builder.toString()); timeStemp = date.getTime(); } catch (ParseException e) { e.printStackTrace(); } return timeStemp.toString(); } /** * 将匹配横杠 或斜杠正则匹配的时间解析后,封装进map * * @param dateStr 源时间字符串 * @param typeStr 源时间字符串的类型 * @return 解析后的map格式的时间 */ private Map<String, String> rodTime(String dateStr, String typeStr) { Map map = new HashMap(7); //已经包含年月日,先截取出来进行解析 String[] arr = dateStr.split(" "); String yearSum = arr[0]; String[] dateArr = yearSum.split(typeStr); map.put("year", dateArr[0]); map.put("month", dateArr[1]); map.put("day", dateArr[2]); if (!StringUtils.isEmpty(arr[1]) && arr[1].contains(":")) { String hourSum = arr[1]; String[] hourArr = hourSum.split(":"); if (!StringUtils.isEmpty(hourArr[0])) { if (!hourArr[0].matches("[0-9]{1,2}")) { map.put("operate", "no"); return map; } map.put("hour", hourArr[0]); } if (!StringUtils.isEmpty(hourArr[1])) { if (!hourArr[1].matches("[0-9]{1,2}")) { map.put("operate", "no"); return map; } map.put("minute", hourArr[1]); } if (!StringUtils.isEmpty(hourArr[2])) { if (!hourArr[2].matches("[0-9]{1,2}")) { map.put("operate", "no"); return map; } map.put("second", hourArr[2]); } } return map; } /** * 将匹配中文正则的时间解析后,封装进map * * @param dateStr 时间字符传 * @return 解析成的map格式的时间 */ private Map<String, String> chineseCharTime(String dateStr) { Map map = new HashMap(7); //已经包含年月日,先截取出来进行解析 String[] arr = dateStr.split(" "); //年月日 String yearSum = arr[0]; String[] yearArr = yearSum.split("年"); map.put("year", yearArr[0]); String monthSum = yearArr[1]; String[] monthArr = monthSum.split("月"); map.put("month", monthArr[0]); String daySum = monthArr[1]; String[] dayArr = daySum.split("日"); map.put("day", dayArr[0]); if (!StringUtils.isEmpty(arr[1]) && arr[1].contains("时")) { String hourSum = arr[1]; String[] hourArr = hourSum.split("时"); if (!hourArr[0].matches("[0-9]{1,2}")) { map.put("operate", "no"); return map; } map.put("hour", hourArr[0]); if (!StringUtils.isEmpty(hourArr[1]) && hourArr[1].contains("分")) { String minSum = hourArr[1]; String[] minArr = minSum.split("分"); if (!minArr[0].matches("[0-9]{1,2}")) { map.put("operate", "no"); return map; } map.put("minute", minArr[0]); if (!StringUtils.isEmpty(minArr[1]) && minArr[1].contains("秒")) { String secondSum = minArr[1]; String[] secondArr = secondSum.split("秒"); if (!secondArr[0].matches("[0-9]{1,2}")) { map.put("operate", "no"); return map; } map.put("second", secondArr[0]); } } } return map; } }