package org.damocode.iot.core.utils;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import java.util.Date;
/**
* @Description: 日期时间工具类
* @Author: zzg
* @Date: 2021/10/7 15:09
* @Version: 1.0.0
*/
public final class DateTimeUtils {
/**
* 年(yyyy)
*/
public static final String YEAR = "yyyy";
/**
* 年-月(yyyy-MM)
*/
public static final String YEAR_MONTH = "yyyy-MM";
/**
* 年-月-日(yyyy-MM-dd)
*/
public static final String YEAR_MONTH_DAY = "yyyy-MM-dd";
/**
* 年月日(yyyyMMdd)
*/
public static final String YEAR_MONTH_DAY_SIMPLE = "yyyyMMdd";
/**
* 年-月-日 小时(yyyy-MM-dd HH)
*/
public static final String YEAR_MONTH_DAY_HOUR = "yyyy-MM-dd HH";
/**
* 年-月-日 小时(yyyy-MM-dd HH)中文输出
*/
public static final String YEAR_MONTH_DAY_HOUR_CN = "yyyy年MM月dd日HH时";
/**
* 年-月-日 小时:分钟(yyyy-MM-dd HH:mm)
*/
public static final String YEAR_MONTH_DAY_HOUR_MINUTE = "yyyy-MM-dd HH:mm";
/**
* 年-月-日 小时:分钟:秒钟(yyyy-MM-dd HH:mm:ss)
*/
public static final String YEAR_MONTH_DAY_HOUR_MINUTE_SECOND = "yyyy-MM-dd HH:mm:ss";
/**
* 年月日小时分钟秒钟(yyyyMMddHHmmss)
*/
public static final String YEAR_MONTH_DAY_HOUR_MINUTE_SECOND_SIMPLE = "yyyyMMddHHmmss";
/**
* 小时:分钟:秒钟(HH:mm:ss)
*/
public static final String HOUR_MINUTE_SECOND = "HH:mm:ss";
/**
* 小时:分钟(HH:mm)
*/
public static final String HOUR_MINUTE = "HH:mm";
/**
* 月.日(M.d)
*/
public static final String MONTH_DAY = "M.d";
/**
* 一天的秒数
*/
private static final int DAY_SECOND = 24 * 60 * 60;
/**
* 一小时的秒数
*/
private static final int HOUR_SECOND = 60 * 60;
/**
* 一分钟的秒数
*/
private static final int MINUTE_SECOND = 60;
/**
* 格式化日期时间
*
* @param date Date对象
* @param pattern 模式
* @return 格式化后的日期时间字符串
*/
public static String format(Date date, String pattern) {
if (date == null)
return "";
return new DateTime(date).toString(pattern);
}
/**
* 格式化日期时间字符串
*
* @param dateString 日期时间字符串
* @param pattern 模式
* @return Date对象
*/
public static Date formatDateString(String dateString, String pattern) {
try {
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(pattern);
return dateTimeFormatter.parseDateTime(dateString).toDate();
} catch (Exception e) {
return null;
}
}
/**
* 根据秒数获得x天x小时x分钟x秒字符串
*
* @param second 秒数
* @return x天x小时x分钟x秒字符串
*/
public static String getDayHourMinuteSecond(int second) {
if (second == 0) {
return "0秒";
}
StringBuilder sb = new StringBuilder();
int days = second / DAY_SECOND;
if (days > 0) {
sb.append(days);
sb.append("天");
second -= days * DAY_SECOND;
}
int hours = second / HOUR_SECOND;
if (hours > 0) {
sb.append(hours);
sb.append("小时");
second -= hours * HOUR_SECOND;
}
int minutes = second / MINUTE_SECOND;
if (minutes > 0) {
sb.append(minutes);
sb.append("分钟");
second -= minutes * MINUTE_SECOND;
}
if (second > 0) {
sb.append(second);
sb.append("秒");
}
return sb.toString();
}
/**
* 根据秒数获得x天x小时x分钟字符串
*
* @param second 秒数
* @return x天x小时x分钟字符串
*/
public static String getDayHourMinute(int second) {
if (second == 0) {
return "0分钟";
}
StringBuilder sb = new StringBuilder();
int days = second / DAY_SECOND;
if (days > 0) {
sb.append(days);
sb.append("天");
second -= days * DAY_SECOND;
}
int hours = second / HOUR_SECOND;
if (hours > 0) {
sb.append(hours);
sb.append("小时");
second -= hours * HOUR_SECOND;
}
int minutes = second / MINUTE_SECOND;
if (minutes > 0) {
sb.append(minutes);
sb.append("分钟");
}
return sb.toString();
}
/**
* 获取只含有年月日的DateTime对象
*
* @param dateTime DateTime对象
* @return 只含有年月日的DateTime对象
*/
public static DateTime getDateOnly(DateTime dateTime) {
return new DateTime(dateTime.toString(YEAR_MONTH_DAY));
}
/**
* 获取当前周的周一和下周一
*
* @return 日期数组(索引0为周一,索引1为下周一)
*/
public static Date[] getMondayAndNextMonday() {
DateTime dateTime = getDateOnly(new DateTime());
DateTime monday = dateTime.dayOfWeek().withMinimumValue();
DateTime nextMonday = monday.plusDays(7);
return new Date[]{monday.toDate(), nextMonday.toDate()};
}
/**
* 获取指定时间的周一和周日
*
* @param dateTime DateTime对象
* @return 日期数组(索引0为周一,索引1为周日)
*/
public static Date[] getMondayAndSunday(DateTime dateTime) {
dateTime = getDateOnly(dateTime);
DateTime monday = dateTime.dayOfWeek().withMinimumValue();
DateTime sunday = monday.plusDays(6);
return new Date[]{monday.toDate(), sunday.toDate()};
}
/**
* 和当前时间相比的天数差(正数为大于天数,负数为小于天数,零为同一天)
*
* @param date Date对象
* @return 和当前时间相比的天数差
*/
public static int compareDaysWithNow(Date date) {
return Days.daysBetween(new DateTime(), new DateTime(date)).getDays();
}
/**
* 和今天相比的天数差(正数为大于天数,负数为小于天数,零为同一天)
*
* @param date Date对象
* @return 和今天相比的天数差
*/
public static int compareDaysWithToday(Date date) {
DateTime today = new DateTime();
today = new DateTime(today.getYear(), today.getMonthOfYear(), today.getDayOfMonth(), 0, 0, 0, 0);
DateTime compareDay = new DateTime(date);
compareDay = new DateTime(compareDay.getYear(), compareDay.getMonthOfYear(), compareDay.getDayOfMonth(), 0, 0, 0, 0);
return Days.daysBetween(today, compareDay).getDays();
}
/**
* 比较时间a到时间b的天数差
*
* @param a 时间a
* @param b 时间b
* @return 相差天数
*/
public static int compareDaysWithDay(Date a, Date b) {
DateTime today = new DateTime(b);
today = new DateTime(today.getYear(), today.getMonthOfYear(), today.getDayOfMonth(), 0, 0, 0, 0);
DateTime compareDay = new DateTime(a);
compareDay = new DateTime(compareDay.getYear(), compareDay.getMonthOfYear(), compareDay.getDayOfMonth(), 0, 0, 0, 0);
return Days.daysBetween(today, compareDay).getDays();
}
/**
* 比较两个时间是否相等(省略毫秒)
*
* @param date Date对象
* @param compareDate 比较Date对象
* @return 是否相等
*/
public static boolean compareDateIgnoreMillisecond(Date date, Date compareDate) {
if (date == null && compareDate == null) {
return true;
} else if (date