com.jcohy.date.DateUtils Maven / Gradle / Ivy
The newest version!
package com.jcohy.date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Copyright : 2015-2033 Beijing
* Created by jiac on 2018/3/5 11:21.
* ClassName : DateUtils
* Description :
*/
public class DateUtils {
/** 年-月-日 时:分:秒 显示格式 */
// 备注:如果使用大写HH标识使用24小时显示格式,如果使用小写hh就表示使用12小时制格式。
private static String[] dateTimeReg = {
"\\s*\\d{1,4}-\\d{1,2}-\\d{1,2}\\s+\\d{1,2}:\\d{1,2}:\\d{1,2}\\s*",
"\\s*\\d{1,4}-\\d{1,2}-\\d{1,2}\\s+\\d{1,2}:\\d{1,2}\\s*",
"\\s*\\d{1,4}-\\d{1,2}-\\d{1,2}\\s+\\d{1,2}\\s*",
"\\s*\\d{1,4}-\\d{1,2}-\\d{1,2}\\s*",
"\\s*\\d{1,4}/\\d{1,2}/\\d{1,2}\\s+\\d{1,2}:\\d{1,2}:\\d{1,2}\\s*",
"\\s*\\d{1,4}/\\d{1,2}/\\d{1,2}\\s+\\d{1,2}:\\d{1,2}\\s*",
"\\s*\\d{1,4}/\\d{1,2}/\\d{1,2}\\s+\\d{1,2}\\s*",
"\\s*\\d{1,4}/\\d{1,2}/\\d{1,2}\\s*"
};
//多种日期格式
private static DateFormat[] dateFormat = {
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"),
new SimpleDateFormat("yyyy-MM-dd HH:mm"),
new SimpleDateFormat("yyyy-MM-dd HH"),
new SimpleDateFormat("yyyy-MM-dd"),
new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"),
new SimpleDateFormat("yyyy/MM/dd HH:mm"),
new SimpleDateFormat("yyyy/MM/dd HH"),
new SimpleDateFormat("yyyy/MM/dd"),
};
/**
* 检查两个时间是否是同一天
* Mar 2002 13:45 and 28 Mar 2002 06:01 would return true.
* 28 Mar 2002 13:45 and 12 Mar 2002 13:45 would return false.
* @param date1 时间1
* @param date2 时间2
* @return 如果代表同一天 返回true
*/
public static boolean isSameDate(Date date1,Date date2){
if(date1 == null || date2 == null){
throw new IllegalArgumentException("The date must not be null");
}
final Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
final Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
return isSameDay(cal1, cal2);
}
/**
* 检查两个时间是否是同一天
* Mar 2002 13:45 and 28 Mar 2002 06:01 would return true.
* 28 Mar 2002 13:45 and 12 Mar 2002 13:45 would return false.
* @param cal1 时间1
* @param cal2 时间2
* @return 如果代表同一天 返回true
*/
public static boolean isSameDay(Calendar cal1,Calendar cal2){
if (cal1 == null || cal2 == null) {
throw new IllegalArgumentException("The date must not be null");
}
return cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);
}
/**
* 检查两个时间是否是同一时间
* @param date1 时间1
* @param date2 时间2
* @return 如果代表同一毫秒 返回true
*/
public static boolean isSameInstant(final Date date1, final Date date2) {
if (date1 == null || date2 == null) {
throw new IllegalArgumentException("The date must not be null");
}
return date1.getTime() == date2.getTime();
}
/**
* 检查两个时间是否是同一时间
* @param cal1 时间1
* @param cal2 时间2
* @return 如果代表同一毫秒 返回true
*/
public static boolean isSameInstant(final Calendar cal1, final Calendar cal2) {
if (cal1 == null || cal2 == null) {
throw new IllegalArgumentException("The date must not be null");
}
return cal1.getTime().getTime() == cal2.getTime().getTime();
}
/**
* 检查两个时间是否代表同一时区时间
* @param cal1 时间1
* @param cal2 时间2
* @return result
*/
public static boolean isSameLocalTime(final Calendar cal1, final Calendar cal2) {
if (cal1 == null || cal2 == null) {
throw new IllegalArgumentException("The date must not be null");
}
return cal1.get(Calendar.MILLISECOND) == cal2.get(Calendar.MILLISECOND) &&
cal1.get(Calendar.SECOND) == cal2.get(Calendar.SECOND) &&
cal1.get(Calendar.MINUTE) == cal2.get(Calendar.MINUTE) &&
cal1.get(Calendar.HOUR_OF_DAY) == cal2.get(Calendar.HOUR_OF_DAY) &&
cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR) &&
cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
cal1.getClass() == cal2.getClass();
}
/**
* @param date 日期
* @return result
*/
private static boolean validateDateNotNull(final Date date) {
if( date != null){
return true;
}else{
throw new IllegalArgumentException("The date must not be null");
}
}
//Converts
//-----------------------------------------------------------------------
/**
* 获取待匹配的字符串对应的正则表达式的下标 index
* @param dateStr 日期字符串
* @return result
*/
public static int getRegIndex(String dateStr){
Pattern pattern = null;
int i=0;
for (; i" + charge_start_date);
// System.out.println("charge_end_date>" + charge_end_date);
// System.out.println("between day is-->" + betweendays);
return result;
}
/**
* 获取中文日期
* @param date 日期
* @return result
*/
public static String getChineseWeek(Calendar date) {
final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
int dayOfWeek = date.get(Calendar.DAY_OF_WEEK);
// System.out.println(dayNames[dayOfWeek - 1]);
return dayNames[dayOfWeek - 1];
}
/**
* 获得日期的下一个星期一的日期
* @param date
* @return
*/
private static Calendar getNextMonday(Calendar date) {
Calendar result = null;
result = date;
do {
result = (Calendar) result.clone();
result.add(Calendar.DATE, 1);
} while (result.get(Calendar.DAY_OF_WEEK) != 2);
return result;
}
/**
* 获取休息日
* @param d1 时间1
* @param d2 时间2
* @return result
*/
public static int getHolidays(Calendar d1, Calendar d2) {
return getDaysBetween(d1, d2) - getWorkingDay(d1, d2);
}
/**
* 获取过去的天数
* @param date 时间
* @return result
*/
public static long getPastDays(Date date) {
long t = System.currentTimeMillis()-date.getTime();
return t/(24*60*60*1000);
}
/**
* 获取过去的小时
* @param date 时间
* @return result
*/
public static long getPastHour(Date date) {
long t = System.currentTimeMillis()-date.getTime();
return t/(60*60*1000);
}
/**
* 获取过去的分钟
* @param date 时间
* @return result
*/
public static long getPastMinutes(Date date) {
long t = System.currentTimeMillis()-date.getTime();
return t/(60*1000);
}
/**
* 获取过去的秒
* @param date 时间
* @return result
*/
public static long getPastSeconds(Date date) {
long t = System.currentTimeMillis()-date.getTime();
return t/(1000);
}
}