import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
/**
* 格式化date
* @param date yyyy-MM-dd HH:mm:ss
* @return 字符格式时间戳
*/
public static String formatDate(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter.format(date);
}
/**
* 格式化简短时间戳
* @param date 时间
* @return 格式化时间戳
*/
public static String formatMonthShortDate(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMM");
return formatter.format(date);
}
/**
* 格式化date
* @param date 日期
* @return 字符格式时间戳
*/
public static String formatDayShortDate(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(date);
}
/**
* 获取当天零点时间
* @return 当天零点时间
*/
public static Date getZeroTime() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
/**
* 获取一周前的时间
* @return 一周前的时间
*/
public static Date getWeekBeforeTime() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.add(Calendar.DATE, -7);
return calendar.getTime();
}
/**
* 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
* @return 日期
*/
public static Date strToDate(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
return formatter.parse(strDate, pos);
}
/**
* 将长时间格式字符串转换为时间 yyyy-MM-dd
* @param strDate 日期格式
* @return 日期
*/
public static Date strToSortDate(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
return formatter.parse(strDate, pos);
}
/**
* 获取当前指定时间日期
* @param hour 小时
* @param minute 分钟
* @return 日期
*/
public static Date getSpecifiedDate(int hour, int minute) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
/**
* 获取当前指定时间日期
* @param hour 小时
* @param minute 分钟
* @return 日期
*/
private static Date getSpecifiedDate(Date date, int hour, int minute) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
/**
* 获取当天零点时间
* @return 当天零点时间
*/
public static Date getZeroTime(Date currentDate) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(currentDate);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
/**
* 获取当天最后时间
* @return 最后时间
*/
public static Date getLastTime() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
return calendar.getTime();
}
/**
* 得到传入日期n天后的日期,如果传入日期为null,则表示当前日期n天后的日期
* @param days 可以为任何整数,负数表示前days天,正数表示后days天
* @return Date
*/
public static Date getAddDayDate(Date dt, int days) {
Calendar cal = Calendar.getInstance();
if (dt != null) {
cal.setTime(dt);
}
cal.add(Calendar.DAY_OF_MONTH, days);
return cal.getTime();
}
/**
* 获取若干个小时之后的时间(字符串类型)
* @param dt date
* @param hours hours
* @return date
*/
public static Date getAddHoursDate(Date dt, int hours) {
// 获取一个小时以后的时间
Calendar calendar = Calendar.getInstance();
if (dt != null) {
calendar.setTime(dt);
}
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + hours);
return calendar.getTime();
}
public static long day2Seconds(Integer day) {
if (day == null) {
throw new IllegalArgumentException("日期天数转换参数不能为空");
}
return day * (60 * 60 * 24);
}
/**
* 获取当天剩余秒数
* @return 剩余多少秒数
*/
public static long getTodayRemainingSeconds() {
LocalDateTime midnight = LocalDateTime.now().plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
return ChronoUnit.SECONDS.between(LocalDateTime.now(), midnight);
}
/**
* 获取当天是周几
* @param date Date
* @return 周几
*/
public static int getDayOfWeek(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_WEEK) - 1;
}
/**
* 获取当天是第几天
* @param date Date
* @return 第几天
*/
public static int getDayOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_MONTH);
}
/**
* 获取下个月的 1月1号
* @param date 时间
* @return date
*/
public static Date getNextMonthFirstDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
/**
* 计算出两天相差多少天
* @param date1 时间
* @param date2 时间
* @return 天数
*/
public static int getDaysBetween(Date date1, Date date2) {
long betweenDays = (date1.getTime() - date2.getTime()) / (1000 * 60 * 60 * 24);
return (int) (betweenDays < 0 ? -betweenDays : betweenDays);
}
}
Date工具类
最新推荐文章于 2020-07-02 12:02:15 发布