一、根据一段时间区间,按月份拆分成多个时间段
package test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.junit.Test;
public class GetDateListUtil {
@Test
public static void main(String[] args) throws ParseException {
List<DateList> list = GetDateListUtil.getDateList("2018-12-23",
"2019-03-01");
for (DateList date : list) {
System.out.println(date.getDate());
}
}
/**
* 根据一段时间区间,按月份拆分成多个时间段
*
* @param startDate
* 开始日期
* @param endDate
* 结束日期
* @return
*/
@SuppressWarnings("deprecation")
public static List<DateList> getDateList(String startDate, String endDate)
throws ParseException {
List<DateList> list = null;
try {
list = new ArrayList<DateList>();
Date dateStart = new SimpleDateFormat("yyyy-MM-dd")
.parse(startDate);// 定义起始日期
Date dateEnd = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);// 定义结束日期
Calendar caleStart = Calendar.getInstance();// 定义开始日期实例
caleStart.setTime(dateStart);// 设置日期起始时间
caleStart.add(Calendar.DAY_OF_MONTH, -1);// 设置日期起始时间往后减去一天
Calendar caleEnd = Calendar.getInstance();// 定义结束日期实例
caleEnd.setTime(dateEnd);// 设置日期结束时间
Calendar cale = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String time = "";
DateList dateList = null;
if (dateStart.before(dateEnd)) {
cale.setTime(caleEnd.getTime());
while (caleStart.getTime().before(cale.getTime())) {
time = sdf.format(cale.getTime());
cale.add(Calendar.DAY_OF_MONTH, -1);
System.out.println(time);
}
} else {
return null;
}
} catch (ParseException e) {
return null;
}
return list;
}
}
二、时间倒序排列
package test;
public class DateList {
private String date;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
package lk.project.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class DateUtil {
public static String defaultDatePattern = "yyyy-MM-dd";
public static String DatePatternForHms = "yyyy-MM-dd HH:mm:ss" ;
public static SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
public static SimpleDateFormat dateTimeFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* * 获得默认的 date pattern
* */
public static String getDatePattern() {
return defaultDatePattern;
}
/**
* * 返回预设Format的当前日期字符串
* */
public static String getToday() {
Date today = new Date();
return format(today);
}
/**
* * 使用预设Format格式化Date成字符串
* */
public static String format(Date date) {
return format(date, getDatePattern());
}
/**
* * 使用参数Format格式化Date成字符串
* */
public static String format(Date date, String pattern) {
String returnValue = "";
if (date != null) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
returnValue = df.format(date);
}
return (returnValue);
}
/**
* * 使用预设格式将字符串转为Date
* */
public static Date parse(String strDate) throws ParseException {
return parse(strDate, getDatePattern());
}
/**
* * 使用参数Format将字符串转为Date
* */
public static Date parse(String strDate, String pattern) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat(pattern);
return df.parse(strDate);
}
/**
* * 返回当前的日期时间字符串 格式:"yyyy-MM-dd HH:mm:ss"
* *
* * @return string
* */
public static String getCurrenDateTime() {
String dt = dateTimeFormatter.format(new Date());
return dt;
}
/**
* 通过Redis的自增来控制编号的自增
* @param
* @return 生成的编码
*/
public static String generateSn(){
String timeStr = DateUtil.format(new Date(),"yyyyMMddhhmmss");//+"15";
return timeStr;
}
/**
* * 返回当前的日期字符串 格式: "yy-MM-dd"
* *
* * @return
* */
public static String getCurrenDate() {
String date = dateFormatter.format(new Date());
return date;
}
/**
* * 根据所传的格式返回当前的时间字符串 格式 pattern
* *
* * @param pattern
* * format pattern
* * @return current datetime
* */
public static String getCurrenDateTimeByPattern(String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
String dt = sdf.format(new Date());
return dt;
}
/**
* * 根据所传的格式,格式化想要处理的date 格式: pattern 如果date == null,则返回当前的date字符串
* *
* * @param date
* * java.util.Date
* * @return short format datetime
* */
public static String dateFormatterByPattern(Date date, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
if (date != null) {
return sdf.format(date);
} else {
return sdf.format(new Date());
}
}
/**
* * 格式化传进去的日期 格式:yyyy-MM-dd HH:mm:ss
* *
* * @param date
* * @return String
* */
public static String dateTimeFormatter(Date date) {
if (date != null) {
return dateTimeFormatter.format(date);
} else {
return dateTimeFormatter.format(new Date());
}
}
/**
* 格式化传进去的日期 格式 :yyyy-MM-dd
* *
* * @param date
* * @return
* */
public static String dateFormatter(Date date) {
if (date != null) {
return dateFormatter.format(date);
} else {
return dateFormatter.format(new Date());
}
}
/**
* * 把传进去的字符串按照相对应的格式转换成日期,时间 格式: "yyyy-MM-dd HH:mm:ss"
* *
* * @param param
* *
* * @return java.util.Date
* */
public static Date dateTimeParser(String param) {
Date date = null;
try {
date = dateTimeFormatter.parse(param);
} catch (ParseException ex) {
}
return date;
}
/**
* * 把传进去的字符串按照相对应的格式转换成日期 格式:yyyy-MM-dd
* *
* * @param param
* * @return
* */
public static Date dateParser(String param) {
Date date = null;
try {
date = dateFormatter.parse(param);
} catch (ParseException ex) {
}
return date;
}
/**
* * 比较两个日期的前后,前面的日期在后,则为true
* *
* * @param date1
* * @param date2
* * @return
* */
public static boolean isDateAfter(Date date1, Date date2) {
return date1.getTime() >= date2.getTime();
}
/**
* * 比较两个日期的前后,前面的日期在后,则为true
* *
* * @param date1
* * @param date2
* * @return
* */
public static boolean isInDate(Date date, Date date1, Date date2) {
return date1.getTime() <= date.getTime() && date2.getTime() >= date.getTime();
}
/**
* * 比较两个日期字符串的先后,前面的日期在后,则为true
* *
* * @param date1
* * * @param date2
* * * @return
* */
public static boolean isDateAfter(String date1, String date2) {
Date datea = toCalendar(date1).getTime();
Date dateb = toCalendar(date2).getTime();
return DateUtil.isDateAfter(datea, dateb);
}
/**
* * 与当前日期比,比较两个日期字符串的先后
* *
* * @param date
* * 要比较的日期
* * @return
* */
public static boolean isDateInvalidation(Date date) {
Date date2 = new Date();
return DateUtil.isDateAfter(date2, date);
}
/**
* * 从一个日期字符串中取出它的年份
* *
* * @param strDate
* * @return
* */
public static final int getYear(Date date) {
Calendar cale = Calendar.getInstance();
cale.setTime(date);
return cale.get(Calendar.YEAR);
}
/**
* * 从一个日期字符串中取出它的年份
* *
* * @param strDate
* * @return
* */
public static final int getYear(String strDate) {
Calendar cale = toCalendar(strDate);
if (cale == null) {
return -1;
}
return cale.get(Calendar.YEAR);
}
/**
* * 从一个日期字符串中取出它的月份
* *
* * @param strDate
* * @return
* */
public static final int getMonth(String strDate) {
Calendar cale = toCalendar(strDate);
if (cale == null) {
return -1;
}
return cale.get(Calendar.MONTH) + 1;
}
/**
* * 从一个日期字符串中取出它的日期
* *
* * @param strDate
* * @return
* */
public static final int getDate(String strDate) {
Calendar cale = toCalendar(strDate);
if (cale == null) {
return -1;
}
return cale.get(Calendar.DATE);
}
/**
* * 把一个日期字符串转换成Calendar形式
* *
* * @param strDate
* * @return
* */
public static final Calendar toCalendar(String strDate) {
Calendar cale = null;
try {
Date thisDate = dateTimeFormatter.parse(strDate);
cale = Calendar.getInstance();
cale.setTime(thisDate);
} catch (Exception e) {
return null;
}
return cale;
}
/**
* * 把一个日期转换成Calendar形式
* *
* * @param strDate
* * @return
* */
public static final Calendar toCalendar(Date date) {
Calendar cale = null;
try {
cale = Calendar.getInstance();
cale.setTime(date);
} catch (Exception e) {
return null;
}
return cale;
}
/**
* * 返回昨天的日期字符串
* *
* * @param strDate
* * @return
* */
public static final String getYesday() {
String strDate = getCurrenDateTime();
Calendar cale = toCalendar(strDate);
cale.add(Calendar.DAY_OF_YEAR, -1);
return dateFormatterByPattern(cale.getTime(), "yyyy-MM-dd");
}
/**
* * 计算出strDate之后days天后的日期字符串 days可以为负数
* *
* * @param strDate
* * @param days
* * @return
* */
public static final String addDayToString(String strDate, int days) {
Calendar cale = toCalendar(strDate);
cale.add(Calendar.DAY_OF_YEAR, days);
return dateFormatterByPattern(cale.getTime(), "yyyy-MM-dd HH:mm:ss");
}
/**
* * 此函数计算出date之后amount分钟的日期
* *
* * @param date
* * @param amount
* * @return
* */
public static Date addMinute(Date date, int amount) {
Calendar cale = Calendar.getInstance();
cale.setTime(date);
cale.add(Calendar.MINUTE, amount);
return cale.getTime();
}
/**
* * 此函数计算出date之后days天后的日期,days可以是负数*
* * @param strDate
* * @param days
* * @return
* */
public static final Date addDay(Date date, int days) {
Calendar cale = Calendar.getInstance();
cale.setTime(date);
cale.add(Calendar.DAY_OF_YEAR, days);
return cale.getTime();
}
/**
* 把日期转换成字符串型
*
* @param date
* @param pattern
* @return
*/
public static String toString(Date date, String pattern) {
if (date == null) {
return "";
}
if (pattern == null) {
pattern = "yyyy-MM-dd";
}
String dateString = "";
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
dateString = sdf.format(date);
} catch (Exception ex) {
ex.printStackTrace();
}
return dateString;
}
public static String toString(Long time, String pattern) {
if (time > 0||time<0) {
if (time.toString().length() == 10) {
time = time * 1000;
}
Date date = new Date(time);
String str = DateUtil.toString(date, pattern);
return str;
}
return "";
}
public static long getDateline(String date) {
return (long) (toDate(date, "yyyy-MM-dd").getTime() / 1000);
}
public static long getDateline() {
return System.currentTimeMillis() / 1000;
}
/**
* 将一个字符串转换成日期格式
*
* @param date
* @param pattern
* @return
*/
public static Date toDate(String date, String pattern) {
if (("" + date).equals("")) {
return null;
}
if (pattern == null) {
pattern = "yyyy-MM-dd";
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date newDate = new Date();
try {
newDate = sdf.parse(date);
} catch (Exception ex) {
ex.printStackTrace();
}
return newDate;
}
/**
* * 此函数计算出指定日期之后moths个月的日期,Months可以是负数
* *
* * @param date
* * @param n
* * @return
* */
public static final Date addMonth(Date date, int months) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, months);
return cal.getTime();
}
/**
* * 此函数计算出指定日期之后seconds秒的日期,seconds可以是负数
* *
* * @param date
* * @param seconds
* * @return
* */
public static final Date addSecond(Date date, int seconds) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.SECOND, seconds);
return cal.getTime();
}
/**
* * 计算指定的两个日期之间的天数
* *
* * @param postDate
* * 之前的时间
* * @param afterDate
* * 之后的时间
* * @return
* */
public static final int diffDay(Date postDate, Date afterDate) {
Calendar past = Calendar.getInstance();
past.setTime(postDate);
Calendar after = new GregorianCalendar();
after.setTime(afterDate);
long diffMillis = after.getTimeInMillis() - past.getTimeInMillis();
return (int) (diffMillis / 1000 / 60 / 60 / 24);
}
/**
* * 计算指定的两个日期之间的小时
* *
* * @param postDate
* * 之前的时间
* * @param afterDate
* * 之后的时间
* * @return
* */
public static final int diffHour(Date postDate, Date afterDate) {
Calendar past = Calendar.getInstance();
past.setTime(postDate);
Calendar after = new GregorianCalendar();
after.setTime(afterDate);
long diffMillis = after.getTimeInMillis() - past.getTimeInMillis();
return (int) (diffMillis / 1000 / 60 / 60);
}
/**
* * 取得指定日期是周几
* *
* * @param date
* * 时间。为null则为当前时间
* * @return
* */
@SuppressWarnings("static-access")
public static final int getWeekIndex(Date date) {
Calendar cal = Calendar.getInstance();
if (date == null) {
cal.setTime(new Date());
} else {
cal.setTime(date);
}
return cal.get(cal.DAY_OF_WEEK) - 1;
}
/**
* * 取得指定日期是几点
* *
* * @param date
* * 时间。为null则为当前时间
* * @return
* */
@SuppressWarnings("static-access")
public static final int getHourIndex(Date date) {
Calendar cal = Calendar.getInstance();
if (date == null) {
cal.setTime(new Date());
} else {
cal.setTime(date);
}
return cal.get(cal.HOUR_OF_DAY);
}
/**
* * 取得指定日期的当月的第一天
* *
* * @param date
* * 时间。为null则为当前时间
* * @return
* */
@SuppressWarnings("static-access")
public static Date getFirstOfMonth(Date date) {
Calendar cal = Calendar.getInstance();
if (date == null) {
cal.setTime(new Date());
} else {
cal.setTime(date);
}
cal.set(cal.DAY_OF_MONTH, 1);
cal.set(cal.HOUR_OF_DAY, 0);
cal.set(cal.MINUTE, 0);
cal.set(cal.SECOND, 0);
return cal.getTime();
}
/**
* * 取得指定日期的当月的最后一天
* *
* * @param date
* * 时间。为null则为当前时间
* * @return
* */
@SuppressWarnings("static-access")
public static Date getEndOfMonth(Date date) {
Calendar cal = Calendar.getInstance();
if (date == null) {
cal.setTime(new Date());
} else {
cal.setTime(date);
}
cal.set(cal.DAY_OF_MONTH, cal.getActualMaximum(cal.DAY_OF_MONTH));
cal.set(cal.HOUR_OF_DAY, 0);
cal.set(cal.MINUTE, 0);
cal.set(cal.SECOND, 0);
return cal.getTime();
}
/**
* * 取得指定日期的当天的0点0分0秒
* *
* * @param date
* * 时间。为null则为当前时间
* * @return
* */
@SuppressWarnings("static-access")
public static final Date getFirstOfDay(Date date) {
Calendar cal = Calendar.getInstance();
if (date == null) {
cal.setTime(new Date());
} else {
cal.setTime(date);
}
cal.set(cal.HOUR_OF_DAY, 0);
cal.set(cal.MINUTE, 0);
cal.set(cal.SECOND, 0);
return cal.getTime();
}
/**
* * 取得指定日期的当天的23点59分59秒
* *
* * @param date
* * 时间。为null则为当前时间
* * @return
* */
@SuppressWarnings("static-access")
public static final Date getEndOfDay(Date date) {
Calendar cal = Calendar.getInstance();
if (date == null) {
cal.setTime(new Date());
} else {
cal.setTime(date);
}
cal.set(cal.HOUR_OF_DAY, 23);
cal.set(cal.MINUTE, 59);
cal.set(cal.SECOND, 59);
return cal.getTime();
}
/**
* * 获取指定某年有多少天
* *
* * @param year
* *
* * @return
* */
public static final int getMaxDaysOfYear(int year) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
return cal.getActualMaximum(Calendar.DAY_OF_YEAR);
}
/**
* * 返回当前时间的字符串,格式“yyyyMMdd”
* *
* * @return
* */
public static final String getCurrenDateStr() {
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyyMMdd");
return dateFormatter.format(new Date());
}
/**
* * 返回当前时间的字符串,格式“yyyyMMdd”
* *
* * @return
* */
public static final String getCurrenTimeStr() {
SimpleDateFormat dateFormatter = new SimpleDateFormat("hhmmss");
return dateFormatter.format(new Date());
}
/**
* 获取现在时间
*
* @return返回长时间格式 yyyy-MM-dd HH:mm:ss
*/
public static Date getSqlDate() {
Date sqlDate = new java.sql.Date(new Date().getTime());
return sqlDate;
}
/**
* 获取明天时间
* @return
*/
public static Date getTomorrowDate() {
Date date=new Date();//取时间
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动
date=calendar.getTime(); //这个时间就是日期往后推一天的结果
return date;
}
/**
* 查找指定时间的 周一到周日时间
* @param date 指定时间
* @param num 一周第几天 例如 周日传6 n-1
* @return
*/
public static Date getWeekDate(Date date,int num){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
Calendar cal = Calendar.getInstance();
cal.setTime(date);
//判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
if(1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
cal.setFirstDayOfWeek(Calendar.MONDAY);//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
int day = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);//根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
cal.add(Calendar.DATE, num);
System.out.println("所在周星期日的日期:"+sdf.format(cal.getTime()));
return date;
}
/**
* 获取往后推多少天的时间
* @param num
* @return
*/
public static Date getAfterDate(int num){
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, num);
Date date = cal.getTime();
return date;
}
/**
* 时间戳转换成日期格式字符串
* @param seconds 精确到秒的字符串
* @param formatStr
* @return
*/
public static String timeStamp2Date(String seconds,String format) {
if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
return "";
}
if(format == null || format.isEmpty()){
format = "yyyy-MM-dd HH:mm:ss";
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new Date(Long.valueOf(seconds+"000")));
}
public static void main(String[] args) {
System.out.println(getWeekDate(new Date(),6));
}
}
三、正则表达式工具类-传各种日期格式均转换为yyyy-MM-dd hh:mm:ss
package com.goodwill.upv.util;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
/**
* 正则表达式工具类-传各种日期格式均转换为yyyy-MM-dd hh:mm:ss
* @author sun
*
*/
public class TimeRegexUtils {
/**
* 判断是否是指定的日期格式(yyyymmddhhmmss)
* @param
* @return
*/
public static boolean yyyymmddhhmmssValidate(String regexStr) {
//String pattern = "\\d{4}-(0[1-9]|1[1-2])-(0[1-9]|2[0-9]|3[0-1])";
String pattern = "^\\d{14}$";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(regexStr);
if (m.matches()) {
return true;
} else {
return false;
}
}
/**
* 参考专用
* @param dateStr
* \\D+:非数字字符一次或多次
* @return
*/
@SuppressWarnings("finally")
public static String regexFormatDate(String dateStr) {
dateStr = dateStr.trim();
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 2000-05-30-09-05-00
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+\\d{1,4}$",
"yyyy-MM-dd-HH-mm-ss");//2018-05-28 10:50:05.8 2018-05-28 10:50:05.18 2018-05-28 10:50:05.838 2018-05-28 10:50:05.8383
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(日.月.年)
dateRegFormat.put("^\\d{4}-\\d{1,2}-\\d{1,2}[a-zA-Z]", "yyyy-MM-dd");//2019-01-02Z (带字母)
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 {
if (StringUtils.isBlank(strSuccess)) {
strSuccess = null;
}
return strSuccess;
}
}
public static void main(String[] args) {
//System.out.println(regexFormatDate("2000-05-30-09-05-23"));
//System.out.println(regexFormatDate("2000-05-30"));
// System.out.println("===" + regexFormatDate("2010-02-01 64 "));
// System.out.println("===" + regexFormatDate("2009-11-18 11:39"));
System.out.println("===" + regexFormatDate("2018-05-28 10:50:05.8"));
// 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())+ regexFormatDate(dateStrArray[i]));
// }
}
}