public class DateUtils {
private DateUtils() {
}
public static final String startDay = " 00:00:00";
public static final String endDay = " 23:59:59";
private static final String[] parsePatterns = {
"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM",
"yyyyMMdd", "yyyyMMddHHmmss", "yyyyMMddHHmm", "yyyyMM", "yyyy"
};
public static Long currentSecond() {
return ConvertUtil.ms2s(System.currentTimeMillis());
}
public static Long currentMillis() {
return System.currentTimeMillis();
}
/**
* 毫秒级时间戳转为日期字符串
*
* @param time
* @param format
* @return
*/
public static String toDateTime(Long time, String format) {
DateTime date = DateUtil.date(time);
return DateUtil.format(date, format);
}
/**
* 日期字符串转为毫秒级时间戳
*
* @param dateStr
* @return
*/
public static Long strToMs(String dateStr) {
DateTime date = DateUtil.parse(dateStr);
return date.getTime();
}
public static DateTime endOfDay(String dateStr) {
DateTime date = DateUtil.parse(dateStr);
return DateUtil.endOfDay(date);
}
/**
* 获取每天开始时间
*
* @param dateStr
* @return
*/
public static DateTime beginOfDay(String dateStr) {
DateTime date = DateUtil.parse(dateStr);
return DateUtil.beginOfDay(date);
}
/**
* 日期字符串转为秒级时间戳
*
* @param dateStr
* @return
*/
public static Long strTos(String dateStr) {
DateTime date = DateUtil.parse(dateStr);
return ConvertUtil.ms2s(date.getTime());
}
/**
* 自然月偏移(计费出账业务使用)
*
* @param dateStr
* @param offset
* @return
*/
public static DateTime getTimeOffsetNaturalMonth(String dateStr, Integer offset) {
return getTimeOffsetNaturalMonth(strToMs(dateStr), offset);
}
/**
* 自然月偏移(计费出账业务使用)
*
* @param time (毫秒级时间戳)
* @param offset
* @return
*/
public static DateTime getTimeOffsetNaturalMonth(Long time, Integer offset) {
return getTimeOffsetNatural(time, DateField.MONTH, offset);
}
/**
* 自然年偏移(计费出账业务使用)
*
* @param dateStr
* @param offset
* @return
*/
public static DateTime getTimeOffsetNaturalYear(String dateStr, Integer offset) {
return getTimeOffsetNaturalYear(strToMs(dateStr), offset);
}
/**
* 按照天偏移(计费出账业务使用)
*
* @param time (毫秒级时间戳)
* @param offset
* @return
*/
public static DateTime getTimeOffsetNaturalDay(Long time, Integer offset) {
DateTime date = DateUtil.date(time);
return DateUtil.beginOfDay(DateUtil.offset(date, DateField.DAY_OF_YEAR, offset));
}
/**
* 按照天偏移(计费出账业务使用)
*
* @param dateStr
* @param offset
* @return
*/
public static DateTime getTimeOffsetNaturalDay(String dateStr, Integer offset) {
return getTimeOffsetNaturalDay(strToMs(dateStr), offset);
}
/**
* 自然年偏移(计费出账业务使用)
*
* @param time (毫秒级时间戳)
* @param offset
* @return
*/
public static DateTime getTimeOffsetNaturalYear(Long time, Integer offset) {
return getTimeOffsetNatural(time, DateField.YEAR, offset);
}
/**
* 时间偏移(业务定义自然月)
*
* @param time (毫秒级时间戳)
* @param dateField
* @param offset (正数表示往后偏移,负数表示往前偏移)
* @return
*/
public static DateTime getTimeOffsetNatural(Long time, DateField dateField, Integer offset) {
DateTime date1 = DateUtil.date(time);
DateTime date2 = DateUtil.endOfMonth(date1);
//结束日
DateTime dateTime1;
if (date1.dayOfMonth() == date2.dayOfMonth()) {
dateTime1 = DateUtil.beginOfDay(DateUtil.endOfMonth(DateUtil.offset(date1, dateField, offset)));
} else {
dateTime1 = DateUtil.beginOfDay(DateUtil.offset(date1, dateField, offset));
}
return dateTime1;
}
public static String dealDateFormat(String oldDate) throws ParseException {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
// 设置北京时区
df.setTimeZone(TimeZone.getTimeZone("ETC/GMT-8"));
Date date = df.parse(oldDate);
SimpleDateFormat df1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
Date date1 = df1.parse(date.toString());
DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df2.format(date1);
}
/**
* 比较一段时间是否在另一段时间内
*
* @param startTime 开始时间
* @param endTime 结束时间
* @param productOpenTime 开始时间
* @param productEndTime 结束时间
* @return Boolean
*/
public static Boolean compareTime(Long startTime, Long endTime, Long productOpenTime, Long productEndTime) {
return productOpenTime >= startTime && productEndTime <= endTime;
}
/**
* 判断当前时间是否在固定时间范围内
*
* @param startTime 开始时间
* @param endTime 结束时间
* @return Boolean
*/
public static Boolean compareTimeOne(Long startTime, Long endTime) {
Long currentTime = getCurrentTime();
return currentTime >= startTime && currentTime <= endTime;
}
/**
* 获取当前日期时间戳
*
* @return 当前日期时间戳
*/
public static Long getCurrentTime() {
LocalDate date = LocalDate.now();
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return strToMs(date.format(formatter1));
}
}
05-06
195
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)