全部是代码
1.日期格式规范
public static final String PATTERNSTR_YMD ="(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)";
public static final String PATTERNSTR_YMD_H ="(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))) (0\\d{1}|1\\d{1}|2[0-3]))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29 (0\\d{1}|1\\d{1}|2[0-3]))";
public static final String PATTERNSTR_YMD_HM ="(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))) (0\\d{1}|1\\d{1}|2[0-3]):([0-5]\\d{1})$)|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29 (0\\d{1}|1\\d{1}|2[0-3]):([0-5]\\d{1})$)";
public static final String PATTERNSTR_YMD_HMS="(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))) (0\\d{1}|1\\d{1}|2[0-3]):([0-5]\\d{1}):([0-5]\\d{1})$)|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29 (0\\d{1}|1\\d{1}|2[0-3]):([0-5]\\d{1}):([0-5]\\d{1})$)";
public static final String PATTERNSTR_YMD2 ="(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})/(((0[13578]|1[02])/(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)/(0[1-9]|[12][0-9]|30))|(02/(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))/02/29)";
public static final String PATTERNSTR_YMD_H2 ="(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})/(((0[13578]|1[02])/(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)/(0[1-9]|[12][0-9]|30))|(02/(0[1-9]|[1][0-9]|2[0-8]))) (0\\d{1}|1\\d{1}|2[0-3]))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))/02/29 (0\\d{1}|1\\d{1}|2[0-3]))";
public static final String PATTERNSTR_YMD_HM2 ="(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})/(((0[13578]|1[02])/(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)/(0[1-9]|[12][0-9]|30))|(02/(0[1-9]|[1][0-9]|2[0-8]))) (0\\d{1}|1\\d{1}|2[0-3]):([0-5]\\d{1})$)|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))/02/29 (0\\d{1}|1\\d{1}|2[0-3]):([0-5]\\d{1})$)";
public static final String PATTERNSTR_YMD_HMS2 ="(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})/(((0[13578]|1[02])/(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)/(0[1-9]|[12][0-9]|30))|(02/(0[1-9]|[1][0-9]|2[0-8]))) (0\\d{1}|1\\d{1}|2[0-3]):([0-5]\\d{1}):([0-5]\\d{1})$)|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))/02/29 (0\\d{1}|1\\d{1}|2[0-3]):([0-5]\\d{1}):([0-5]\\d{1})$)";
public static final String PATTERNSTR_YM="(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-((0[1-9])|(1[0-2])))";
public static final String PATTERNSTR_Y="([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})";
public static final String PATTERNSTR_HMS="((0\\d{1}|1\\d{1}|2[0-3]):([0-5]\\d{1}):([0-5]\\d{1})$)";
public static final String PATTERNSTR_HM="((0\\d{1}|1\\d{1}|2[0-3]):([0-5]\\d{1})$)";
public static final String PATTERNSTR_H="((0\\d{1}|1\\d{1}|2[0-3])$)";
public static final String DateMode_1 = "yyyy-MM-dd";
public static final String DateMode_2 = "yyyy-MM-dd HH:mm";
public static final String DateMode_3 = "yyyyMMddHHmmssSSS";
public static final String DateMode_4 = "yyyy-MM-dd HH:mm:ss";
public static final String DateMode_5 = "HHmmssSS";
public static final String DateMode_6 = "yyyyMMdd";
public static final String DateMode_7 = "HHmmss";
public static final String DateMode_8 = "yyyy";
public static final String DateMode_9 = "yyyyMMddHHmmss";
public static final String DateMode_10 = "HH:mm:ss";
public static final String DateMode_11 = "yyyy年MM月dd日HH:mm:ss";
public static final String DateMode_12 = "MM-dd";
public static final SimpleDateFormat date_format = new SimpleDateFormat(DateMode_1);
public static final SimpleDateFormat date_format_str = new SimpleDateFormat(DateMode_6);
public static final SimpleDateFormat date_format_allstr = new SimpleDateFormat(DateMode_9);
public static final SimpleDateFormat datetime_format = new SimpleDateFormat(DateMode_4);
public static final SimpleDateFormat now_time = new SimpleDateFormat(DateMode_10);
public static final String DATE_REGEX = "\\d{4}-\\d{1,2}-\\d{1,2}";
public static final String DEFAULT_DATE_FORMAT = DateMode_1;
2. 格式化制定的date
public static String formateDate(Date date, SimpleDateFormat simpleDateFormat) {
if (date == null) {
return null;
}
if(simpleDateFormat==null){
return null;
}
return simpleDateFormat.format(date);
}
public static String formateDate(Date date,String simpleDateFormatStr) {
if (date == null) {
return null;
}
if(simpleDateFormatStr==null){
simpleDateFormatStr =DEFAULT_DATE_FORMAT;
}
return formateDate(date,new SimpleDateFormat(simpleDateFormatStr));
}
3.获取某天的最小时间
public static Date getDateMin(Date date) {
if(date==null) {
return date;
}
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 1);
c.set(Calendar.MILLISECOND, 0);
return c.getTime();
}
4.获取某天的最大时间
public static Date getDateMax(Date date) {
if(date==null) {
return date;
}
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
c.set(Calendar.MILLISECOND, 999);
return c.getTime();
}
5.获取某天的最大时间插入数据库时
public static Date getDateMaxDB(Date date) {
if(date==null) {
return date;
}
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
c.set(Calendar.MILLISECOND, 0);
return c.getTime();
}
6.startDate加上days是否小于endate,超过返回true,否则返回false
public static boolean compareDate(Date startDate, Date endDate, int days) {
if (startDate == null || endDate == null) {
return false;
}
return addDays(startDate, days).before(endDate);
}
7. startDate加上days是否等于endDate,满足返回true,否则返回false
public static boolean compareDateWithEqual(Date startDate, Date endDate, int days) {
if (startDate == null || endDate == null) {
return false;
}
Date d = addDays(startDate, days);
int r = d.compareTo(endDate);
if (r <= 0) {
return true;
}
return false;
}
8.获取一周中的第几天
public static int getWeekByDate(Date date) {
Calendar c = Calendar.getInstance();
if (date != null) {
c.setTime(date);
}
return c.get(Calendar.DAY_OF_WEEK);
}
9.获取相差几天
public static int getIntervalDayCount(Date sd, Date ed) {
return (int) ((ed.getTime() - sd.getTime()) / (3600 * 24 * 1000) + 1);
}
10.返回两个时间之间的天数
public static Long getDaysBetweenTwoDays(Date startDay, Date endDay) {
return (endDay.getTime() - startDay.getTime()) / (1000 * 60 * 60 * 24);
}
11.获取某个时间段的时间信息
public static List<Date> findDates(Date dBegin, Date dEnd) {
List lDate = new ArrayList();
Calendar calBegin = Calendar.getInstance();
calBegin.setTime(dBegin);
Calendar calEnd = Calendar.getInstance();
calEnd.setTime(dEnd);
while (dEnd.after(calBegin.getTime())) {
lDate.add(calBegin.getTime());
calBegin.add(Calendar.DAY_OF_MONTH, 1);
}
return lDate;
}
public static Date getDate2(String dateStr) throws ParseException{
Date date =null;
if(Pattern.matches(DateUtil.PATTERNSTR_YMD, dateStr)){
SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd");
date=sf1.parse(dateStr);
}else if(Pattern.matches(DateUtil.PATTERNSTR_YMD2, dateStr)){
SimpleDateFormat sf1 = new SimpleDateFormat("yyyy/MM/dd");
date=sf1.parse(dateStr);
}else if(Pattern.matches(DateUtil.PATTERNSTR_YMD_H, dateStr)){
SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd HH");
date=sf1.parse(dateStr);
}else if(Pattern.matches(DateUtil.PATTERNSTR_YMD_H2, dateStr)){
SimpleDateFormat sf1 = new SimpleDateFormat("yyyy/MM/dd HH");
date=sf1.parse(dateStr);
}else if(Pattern.matches(DateUtil.PATTERNSTR_YMD_HM, dateStr)){
SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
date=sf1.parse(dateStr);
}else if(Pattern.matches(DateUtil.PATTERNSTR_YMD_HM2, dateStr)){
SimpleDateFormat sf1 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
date=sf1.parse(dateStr);
}else if(Pattern.matches(DateUtil.PATTERNSTR_YMD_HMS, dateStr)){
SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date=sf1.parse(dateStr);
}else if(Pattern.matches(DateUtil.PATTERNSTR_YMD_HMS2, dateStr)){
SimpleDateFormat sf1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
date=sf1.parse(dateStr);
}
return date;
}
12.验证是否时间
public static boolean isValidDate(String sDate) {
String datePattern1 = "\\d{4}-\\d{2}-\\d{2}";
String datePattern2 = "^((\\d{2}(([02468][048])|([13579][26]))"
+ "[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|"
+ "(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?"
+ "((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?("
+ "(((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?"
+ "((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
if ((sDate != null)) {
Pattern pattern = Pattern.compile(datePattern1);
Matcher match = pattern.matcher(sDate);
if (match.matches()) {
pattern = Pattern.compile(datePattern2);
match = pattern.matcher(sDate);
return match.matches();
} else {
return false;
}
}
return false;
}
13.获取某个时间所在月的最后一天
public static Date getLastDayOfMonth(Date date){
Date lastDayOfMonth = null;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, lastDay);
lastDayOfMonth = cal.getTime();
return lastDayOfMonth;
}
14.获取某个时间所在月的第一天
public static Date getFirstDayOfMonth(Date date){
Date firstDayOfMonth = null;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, 1);
firstDayOfMonth = cal.getTime();
return firstDayOfMonth;
}
15.获取某年的最后一天
public static Date getLastDayOfYear(Date date){
Date lastDayOfMonth = null;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int lastDay = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
cal.set(Calendar.DAY_OF_YEAR, lastDay);
lastDayOfMonth = cal.getTime();
return lastDayOfMonth;
}
16. 获取某年的第一天
public static Date getFirstDayOfYear(Date date){
Date lastDayOfMonth = null;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_YEAR, 1);
lastDayOfMonth = cal.getTime();
return lastDayOfMonth;
}
17.两个时间相差距离多少天多少小时
public static String getDistanceTime2(Date one, Date two) throws ParseException {
long day = 0;
long hour = 0;
long time1 = one.getTime();
long time2 = two.getTime();
long diff ;
if(time1<time2) {
diff = time2 - time1;
} else {
diff = time1 - time2;
}
day = diff / (24 * 60 * 60 * 1000);
hour = (diff / (60 * 60 * 1000) - day * 24);
return day + "天" + hour + "小时" ;
}