相關功能目錄:
- 根據日期轉星期
- 根據日期獲取前後n天的日期
- 根據時間獲取當前日份
- 獲取往前或往後退n個小時的整點時間
- 獲取往前或往之後n分鐘的時間
1.根據日期轉星期
/**
*
* @title getWeekOfDate
* @Description 根據日期轉星期
* @author chenlf
* @param date
* @param type 日期格式
* @return
*/
public static String getWeekOfDate(Date date, String type) {
//type = 1
String[] weekDaysName = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
//type = 2
String[] weekDaysCode = { "七","一", "二", "三", "四", "五", "六" };
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
type = StringUtils.isBlank(type)?"2":"1";
switch (type) {
case "1":
return weekDaysName[intWeek];
case "2":
return weekDaysCode[intWeek];
default:
return weekDaysCode[intWeek];
}
}
2.根據日期獲取前後n天的日期
/**
*
* @title getDate
* @Description 根據日期獲取前後n天的日期
* @author chenlf
* @param date
* @param day
* @return
*/
public static Date getDate(Date date, int day){
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DATE, day);
return c.getTime();
}
3.根據時間獲取當前日份
/**
*
* @title getDay
* @Description 根據時間獲取當前日份
* @author chenlf
* @param date
* @return
*/
public static int getDay(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.DAY_OF_MONTH);
}
4.獲取往前或往後退n個小時的整點時間
/**
*
* @title getHourTime
* @Description 獲取往前或往後退n個小時的整點時間
* eg:
* 13:12 --> 獲取12:00 則 n=-1
* --> 獲取14:00 則 n=1
* @author chenlf
* @param date 時間
* @param n 小時
* @return
*/
public static Date getHourTime (Date date, int n) {
Calendar ca = Calendar.getInstance();
ca.set(Calendar.MINUTE, 0);
ca.set(Calendar.SECOND, 0);
ca.set(Calendar.HOUR_OF_DAY, ca.get(Calendar.HOUR_OF_DAY)+n);
return ca.getTime();
}
5.獲取往前或往之後n分鐘的時間
/**
*
* @title getMinuteTime
* @Description 獲取往前或往之後n分鐘的時間
* @author chenlf
* @param date 時間
* @param min 分鐘
* @return
*/
public static Date getMinuteTime (Date date, int min) {
Calendar ca = Calendar.getInstance();
ca.setTime(date);
ca.add(Calendar.MINUTE, min);
return ca.getTime();
}