mirror of
https://github.com/201206030/novel-plus.git
synced 2025-07-01 15:26:37 +00:00
132 lines
3.5 KiB
Java
132 lines
3.5 KiB
Java
package com.java2nb.common.utils;
|
|
|
|
import lombok.SneakyThrows;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.ArrayList;
|
|
import java.util.Calendar;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* 日期处理
|
|
*/
|
|
public class DateUtils {
|
|
private final static Logger logger = LoggerFactory.getLogger(DateUtils.class);
|
|
public final static String YEAR_PATTERN = "yyyy";
|
|
public final static String MONTH_PATTERN = "MM";
|
|
public final static String DAY_PATTERN = "dd";
|
|
/**
|
|
* 时间格式(yyyy-MM-dd)
|
|
*/
|
|
public final static String DATE_PATTERN = "yyyy-MM-dd";
|
|
/**
|
|
* 时间格式(yyyy-MM-dd HH:mm:ss)
|
|
*/
|
|
public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
|
|
|
|
public static String format(Date date) {
|
|
return format(date, DATE_PATTERN);
|
|
}
|
|
|
|
public static String format(Date date, String pattern) {
|
|
if (date != null) {
|
|
SimpleDateFormat df = new SimpleDateFormat(pattern);
|
|
return df.format(date);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* 计算距离现在多久,非精确
|
|
*
|
|
* @param date
|
|
* @return
|
|
*/
|
|
public static String getTimeBefore(Date date) {
|
|
Date now = new Date();
|
|
long l = now.getTime() - date.getTime();
|
|
long day = l / (24 * 60 * 60 * 1000);
|
|
long hour = (l / (60 * 60 * 1000) - day * 24);
|
|
long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
|
|
long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
|
|
String r = "";
|
|
if (day > 0) {
|
|
r += day + "天";
|
|
} else if (hour > 0) {
|
|
r += hour + "小时";
|
|
} else if (min > 0) {
|
|
r += min + "分";
|
|
} else if (s > 0) {
|
|
r += s + "秒";
|
|
}
|
|
r += "前";
|
|
return r;
|
|
}
|
|
|
|
/**
|
|
* 计算距离现在多久,精确
|
|
*
|
|
* @param date
|
|
* @return
|
|
*/
|
|
public static String getTimeBeforeAccurate(Date date) {
|
|
Date now = new Date();
|
|
long l = now.getTime() - date.getTime();
|
|
long day = l / (24 * 60 * 60 * 1000);
|
|
long hour = (l / (60 * 60 * 1000) - day * 24);
|
|
long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
|
|
long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
|
|
String r = "";
|
|
if (day > 0) {
|
|
r += day + "天";
|
|
}
|
|
if (hour > 0) {
|
|
r += hour + "小时";
|
|
}
|
|
if (min > 0) {
|
|
r += min + "分";
|
|
}
|
|
if (s > 0) {
|
|
r += s + "秒";
|
|
}
|
|
r += "前";
|
|
return r;
|
|
}
|
|
|
|
/**
|
|
* 获取过去第几天的日期
|
|
*
|
|
* @param past
|
|
* @return
|
|
*/
|
|
@SneakyThrows
|
|
public static String getPastDate(int past,Date date) {
|
|
Calendar calendar = Calendar.getInstance();
|
|
calendar.setTime(date);
|
|
calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - past);
|
|
Date today = calendar.getTime();
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
return sdf.format(today);
|
|
}
|
|
|
|
/**
|
|
* 获取过去几天的日期集合
|
|
*
|
|
* @param past
|
|
* @return
|
|
*/
|
|
public static List<String> getDateList(int past,Date date) {
|
|
List<String> result = new ArrayList<>(past);
|
|
for(int i = past - 1 ; i > 0 ; i--){
|
|
result.add(getPastDate(i,date));
|
|
}
|
|
//今天的日期
|
|
result.add(new SimpleDateFormat("yyyy-MM-dd").format(date));
|
|
return result;
|
|
|
|
}
|
|
}
|