前言
最近在項目中對時間處理比較細緻,下面就跟大家介紹一下
具體實現方法
public static final long ONE_DAY = 24 * 60 * 60 * 1000L;
/**
* 判斷該日期是否是該月的第一天
*
* @param date 需要判斷的日期
* @return
*/
public static boolean isFirstDayOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_MONTH) == 1;
}
/**
* 獲取當月第一天
*
* @return
*/
public static Date getMonthFirstDay(Date date, int day) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);//獲取當月第一天
calendar.add(Calendar.MONTH, day);//day: 0是指本月,-1是向前推一個月,1是向後推一個月
return calendar.getTime();
}
/**
* 獲取某年第一天日期
*
* @param year 年份
* @return Date
*/
public static Date getYearFirst(int year) {
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR, year);
Date currYearFirst = calendar.getTime();
return currYearFirst;
}
/**
* 獲取當前年份
*
* @param date
* @return
*/
public static Integer getYear(Date date) {
return (date == null) ? 0 : Integer.parseInt(dateToString(date, "yyyy"));
}
public static Integer getMonth(Date date) {
return (date == null) ? 0 : Integer.parseInt(dateToString(date, "MM"));
}
/**
* 將時間轉換爲時間戳
*
* @param s
* @return
*/
public static String dateToStamp(String s) {
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = simpleDateFormat.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
long ts = (date != null ? date.getTime() : 0) / 1000;
res = String.valueOf(ts);
return res;
}
/**
* 時間戳轉換成日期格式字符串
*
* @param seconds 精確到秒的字符串
* @param
* @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")));
}
/**
* 獲取提前或延後的日期,格式爲yyyy-MM-dd HH:mm
*
* @param dateStr
* @param hour
* @return
*/
public static String getDate(String dateStr, int hour) {
try {
SimpleDateFormat format = new SimpleDateFormat(DateExtendUtil.FULL);
java.util.Date date = format.parse(dateStr);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.HOUR_OF_DAY, hour);
return format.format(cal.getTime());
} catch (Exception e) {
return "";
}
}
/****** 用戶會員過期 **********************************/
//是否已過期
public boolean isExpired(Date vipUntil) {
return vipUntil == null || System.currentTimeMillis() >= vipUntil.getTime();
}
//是否已過期
public boolean isExpired() {
Date vipUntil = getVipUntil();
return vipUntil == null || System.currentTimeMillis() >= vipUntil.getTime();
}
public Date getVipUntil() {
Integer companyId = PrincipalAware.getCompanyId();
if (companyId == null) {
throw new ResException("用戶未登錄");
}
Company company = companyRepository.findOne(companyId);
if (company == null) {
throw new ResException("登錄數據已過期,請重新登錄");
}
return company.getVipUntil();
}
//是否即將到期了
public Map<String, Object> isToExpire() {
Date vipUntil = getVipUntil();
Map<String, Object> map = new HashMap<>();
Boolean flag = false;
Integer days = 0;
if (vipUntil != null) {
long begin = DateExtendUtil.dateBackOrInto(vipUntil, -AuthProperties.WARNING_DAYS).getTime();
long end = vipUntil.getTime();
long now = System.currentTimeMillis();
flag = begin <= now && now < end;//處於2周內
Date expireDay = DateExtendUtil.dateToDate(vipUntil, DateExtendUtil.YEAR);
Date nowDay = DateExtendUtil.dateToDate(new Date(), DateExtendUtil.YEAR);
days = Math.toIntExact((expireDay.getTime() - nowDay.getTime()) / DateExtendUtil.ONE_DAY);
}
map.put("flag", flag);
map.put("days", days);
return map;
}
//是否已過期
public boolean isExpired() {
return isExpired();
}