java 常用工具封装日期类


常用的日期类进行封装,避免以后在调用日期的时候在此写该方法那么会导致项目臃肿。

返回当前星期的Map集合


/*
* 返回参数所在星期的七天,如传入'2021-06-02'星期四,则返回所在星期周一到周日的Map
* @param date
* @return Map
* */
public static HashMap getWeekDates(Date date){
HashMap dates = new HashMap();
Calendar c= Calendar.getInstance();
c.setTime(date);
c.setFirstDayOfWeek(Calendar.MONDAY);
int index = c.get(Calendar.DAY_OF_WEEK);
c.add(Calendar.DAY_OF_WEEK,(-1) * (index - 1));
c.add(Calendar.DAY_OF_WEEK,(-1) * (index - 1));
dates.put("MON",c.getTime());
c.add(Calendar.DAY_OF_WEEK,1);
dates.put("TUE",c.getTime());
c.add(Calendar.DAY_OF_WEEK,1);
dates.put("WED",c.getTime());
c.add(Calendar.DAY_OF_WEEK,1);
dates.put("THU",c.getTime());
c.add(Calendar.DAY_OF_WEEK,1);
dates.put("FRI",c.getTime());
c.add(Calendar.DAY_OF_WEEK,1);
dates.put("SAT",c.getTime());
c.add(Calendar.DAY_OF_WEEK,1);
dates.put("SUN",c.getTime());
return dates;
}



/*
* param date
* pram number
* return 几天后的时间日期
* */
public static Date getDateByTaDay(Date date,int number){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_YEAR,number);
Date nextDay = calendar.getTime();
return nextDay;
}

/*
* param date
* param number 几天后的时间日期
* return 几天后的时间日期去除时分秒
* */
public static Date getDateByYID(Date date,int number){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_YEAR,number);
Date nextDay = calendar.getTime();
nextDay.setHours(0);
nextDay.setMonth(0);
nextDay.setSeconds(0);
return nextDay;
}
/*
* @param beforeDate
* @param lastDate
* @param days 间隔天数
* @return 判断两个日期的间隔天数是否超过参数值 超过返回true 未超过返回false
* */
public static boolean checkDistanceDay(Date beforeDate,Date lastDate,int days){
long interval = 0L;
if (beforeDate.before(lastDate)){
interval = lastDate.getTime() - beforeDate.getTime();
}else{
interval = beforeDate.getTime() - lastDate.getTime();
}
int betweenDays = (int)(interval)/(1000 * 60 * 60 * 24);
if (betweenDays > days){
return true;
}else{
return false;
}
}
/*
* @param Date
* @return 返回参数日期所在季度,1:第一季度 2:第二季度 3:第三季度 4:第四季度
* */
public static int getQuarter(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int month = cal.get(Calendar.MONTH);
switch (month){
case 0:
return 1;
case 1:
return 1;
case 2:
return 1;
case 3:
return 2;
case 4:
return 2;
case 5:
return 2;
case 6:
return 3;
case 7:
return 3;
case 8:
return 3;
case 9:
return 4;
case 10:
return 4;
case 11:
return 4;
default:;
}
return -1;
}

/*
* 把分钟转换成小时
* @param mins
* return 小时
* */
public static String formatMinutesHours(Long mins){
if (mins == null || mins.longValue() == 0){
return "0";
}
return (mins / 60 < 10 ? "0"+mins / 60 : mins / 60)+":"+ StringUtils.leftPad(""+mins % 60,2,"0");
}

/*
* 删除给定Date的时分秒毫秒
* */
public static Date truncateTimeOfDate(Date now){
if ( null == now){
return null;
}
Calendar day = Calendar.getInstance();
day.setTime(now);
day.set(Calendar.HOUR_OF_DAY,0);
day.set(Calendar.MINUTE,0);
day.set(Calendar.SECOND,0);
day.set(Calendar.MILLISECOND,0);
return day.getTime();
}

/*
* 获取两个日期之间的所有日期
* @param startDate
* @param endDate
* @return
* */
public static List getIntervalDates(Date startDate, Date endDate){
List result = new ArrayList();
Long intervalDay = getDateInterval(DataConvertUtil.truncateTimeOfDate(startDate), DataConvertUtil.truncateTimeOfDate(endDate));
if (intervalDay < 0){
return result;
}
for (Long len = 0L ; len <= intervalDay ; len++){
Date currentDate = addDateFieId(startDate, Calendar.DATE, len.intValue());
result.add(currentDate);
}
return result;
}
public static Date addDateFieId(Date now,int fieid,int amount){
if (now == null){
return null;
}
if (amount == 0){
return now;
}
Calendar dar = Calendar.getInstance();
dar.setTime(now);
dar.add(fieid,amount);
return dar.getTime();
}

/*
* 计算当月有多少天
* @param year年
* @param month 月
* */
public static int days(int year,int month){
int days = 0;
if (month != 2){
switch (month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days=31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
}
}else{
if ( year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
days = 29;
}else{
days = 28;
}
}
return days;
}

/*
* 求两个日期差
* @param beginDate 开始日期
* @param endDate 结束日期
* @return 两个日期相差天数
* */
public static Long getDateInterval(Date starDate,Date endDate){
if (starDate.after(endDate)){
return -1L;
}
long interval = endDate.getTime() - starDate.getTime();
return interval / (1000 * 60 * 60 *24);
}

public static String getDataYID(Date date){

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");

String format = simpleDateFormat.format(date);

return format;
}

public static String getDateYIDString(String date) throws ParseException {

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Date parse = simpleDateFormat.parse(date);

return getDataYID(parse);
}


public static String getDataHour(Date date){

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HHmmss");

String format = simpleDateFormat.format(date);

return format;
}

						  
					  

相关