public class DateUtil {
private static final Logger logger = LoggerFactory.getLogger(DateUtil.class);
/**
* 获取给定日期的前一天时间
* @param date
* @return
*/
public static Date getBeforeDate(Date date){
if(date != null){
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).minusDays(1);
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
return null;
}
/**
* 获取给定日期的前二天时间
* @param date
* @return
*/
public static Date getBeforeDate(Date date,int n){
if(date != null){
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).minusDays(n);
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
return null;
}
/**
* 获取给定日期的后一天时间
* @param date
* @return
*/
public static Date getAfterDate(Date date){
if(date != null){
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).plusDays(1);
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
return null;
}
/**
* 获取给定日期的后2天时间
* @param date
* @return
*/
public static Date getAfterAfterDate(Date date){
if(date != null){
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).plusDays(2);
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
return null;
}
/**
* 获取给定日期的上一周的同一天时间
* @param date
* @return
*/
public static Date getBeforeWeek(Date date){
if(date != null){
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).minusWeeks(1);
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
return null;
}
/**
* 获取给定日期的下一周的同一天时间
* @param date
* @return
*/
public static Date getAfterWeek(Date date){
if(date != null){
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).plusWeeks(1);
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
return null;
}
/**
* 获取给定日期的周一
* @param date
* @return
*/
public static Date getMondayOfWeek(Date date){
if(date != null){
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).with(DayOfWeek.MONDAY);
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
return null;
}
/**
* 获取给定日期的周日
* @param date
* @return
*/
public static Date getSundayOfWeek(Date date){
if(date != null){
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).with(DayOfWeek.SUNDAY);
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
return null;
}
/**
* 获取给定日期上个月的同一天
* @param date
* @return
*/
public static Date getBeforeMonth(Date date){
if(date != null){
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).minusMonths(1);
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
return null;
}
/**
* 获取给定日期去年的同一天
* @param date
* @return
*/
public static Date getBeforeYear(Date date){
if(date != null){
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).minusMonths(12);
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
return null;
}
/**
* 获取给定日期的下个月的同一天
* @param date
* @return
*/
public static Date getAfterMonth(Date date){
if(date != null){
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).plusMonths(1);
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
return null;
}
/**
* 获取给定日期的所在月的第一天
* @param date
* @return
*/
public static Date getFirstDayOfMonth(Date date){
if(date != null){
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).with(TemporalAdjusters.firstDayOfMonth());
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
return null;
}
/**
* 获取给定日期的所在月的最后一天
* @param date
* @return
*/
public static Date getLastDayOfMonth(Date date){
if(date != null){
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).with(TemporalAdjusters.lastDayOfMonth());
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
return null;
}
/**
* 给定日期转指定格式字符串
* @param date
* @param format
* @return
*/
public static String dateToFormatString(Date date, String format){
if(date != null){
try {
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern(format));
} catch (Exception e){
e.printStackTrace();
logger.error(e.getMessage());
}
}
return null;
}
/**
* 给定字符串转指定日期
* @param dateStr
* @param format
* @return
*/
public static Date stringToFormatDate(String dateStr, String format){
if(dateStr != null){
SimpleDateFormat sDateFormat=new SimpleDateFormat(format);
Date date = null;
try {
date =sDateFormat.parse(dateStr);
} catch(ParseException px) {
px.printStackTrace();
}
return date;
}
return null;
}
/**
* 获取给定时间的年份
* @param date
* @return
*/
public static Integer getDateYear(Date date){
if(date != null){
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).getYear();
}
return null;
}
/**
* 获取给定时间的月份
* @param date
* @return
*/
public static Integer getDateMonth(Date date){
if(date != null){
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).getMonthValue();
}
return null;
}
/**
* 获取给定时间的月份 共两位,位数不足补零
* @param date
* @return
*/
public static String getDateMonthString(Date date){
if(date != null){
return String.format("%02d", LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).getMonthValue());
}
return null;
}
/**
* 获取给定时间的周的第几天
* @param date
* @return
*/
public static Integer getDayOfWeekByDate(Date date){
if(date != null){
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).getDayOfWeek().getValue();
}
return null;
}
/**
* 获取给定时间的月的第几天
* @param date
* @return
*/
public static Integer getDayOfMonthByDate(Date date){
if(date != null){
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).getDayOfMonth();
}
return null;
}
/**
* 获取给定时间的月的第几天 共两位,位数不足补零
* @param date
* @return
*/
public static String getDateString(Date date){
if(date != null){
return String.format("%02d", getDayOfMonthByDate(date));
}
return null;
}
/**
* 获取给定时间年的第几天
* @param date
* @return
*/
public static Integer getDayOfYearByDate(Date date){
if(date != null){
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).getDayOfYear();
}
return null;
}
/**
* 获取下一年的今天的时间戳
* @return
*/
public static Date getLastYearDate() {
Calendar curr = Calendar.getInstance();
curr.set(Calendar.YEAR,curr.get(Calendar.YEAR)+1);
return curr.getTime();
}
/**
* 获取指定时间的当天起始时间
* @param date
* @return
*/
public static Date getDayStartTime(Date date) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(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();
}
/**
* 获取指定时间的当天起始时间
* @param date
* @return
*/
public static Date getDayEndTime(Date date) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
return calendar.getTime();
}
/**
* 获取当前时间的前多少分钟
*/
public static Date getBeforeMinutes(int n) {
Calendar beforeTime = Calendar.getInstance();
beforeTime.add(Calendar.MINUTE, -n);// 3分钟之前的时间
Date beforeD = beforeTime.getTime();
return beforeD;
}
/**
* 获取当前日期到第二天0点的秒数
* @return
*/
public static Long getSecondsNextEarlyMorning() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, 1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
}
/***
* 获取当前年的第几周
* @param date
* @return
*/
public static String getWeekOfYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.setMinimalDaysInFirstWeek(1);
calendar.setFirstDayOfWeek(Calendar.MONDAY);//设置星期一为一周开始的第一天
int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);//获得当前日期属于今年的第几周
int currentYear = calendar.get(Calendar.YEAR);
/*这里是防止出现周和年不对应的情况 比如2018年12月31日*/
calendar.add(Calendar.DAY_OF_MONTH, 6);
int weekYearByAfter7 = calendar.get(Calendar.YEAR);//6天之后的年份
if(weekYearByAfter7 > currentYear && weekOfYear==1){
currentYear+=1;
}
if(weekOfYear <10){
return ""+currentYear+"0"+weekOfYear;
}
return ""+currentYear+weekOfYear;
}
public static void main(String[] args) {
// Date date = DateUtil.stringToFormatDate("20191230","yyyyMMdd");
// System.out.println(getWeekOfYear(date));
// date = DateUtil.stringToFormatDate("20191231","yyyyMMdd");
// System.out.println(getWeekOfYear(date));
// date = DateUtil.stringToFormatDate("20220106","yyyyMMdd");
// System.out.println(getWeekOfYear(date));
//
System.out.println(getBeforeDate(new Date()));
}
/***
* 取得季度 包含年份
* @param date
* @return
*/
public static String getQuarter(Date date) {
int season = 0;
Calendar c = Calendar.getInstance();
c.setTime(date);
int month = c.get(Calendar.MONTH);
switch (month) {
case Calendar.JANUARY:
case Calendar.FEBRUARY:
case Calendar.MARCH:
season = 1;
break;
case Calendar.APRIL:
case Calendar.MAY:
case Calendar.JUNE:
season = 2;
break;
case Calendar.JULY:
case Calendar.AUGUST:
case Calendar.SEPTEMBER:
season = 3;
break;
case Calendar.OCTOBER:
case Calendar.NOVEMBER:
case Calendar.DECEMBER:
season = 4;
break;
default:
break;
}
int year = c.get(Calendar.YEAR);
return ""+year+"0"+season;
}
public static Date getFirstDayOfYear(Date date) {
if(date != null){
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).with(TemporalAdjusters.firstDayOfYear());
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
return null;
}
public static Date getLastDayOfYear(Date date) {
if(date != null){
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).with(TemporalAdjusters.lastDayOfYear());
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
return null;
}
/***
* 获取季度的第一天
* @param date
* @return
*/
public static Date getFirstDayOfQuarter(Date date) {
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
int month = localDateTime.getMonth().getValue();//当月
int start = 0;
if (month >= 1 && month <= 3) {//第一季度
start = 1;
} else if (month >= 4 && month <= 6) {//第二季度
start = 4;
} else if (month >= 7 && month <= 9) {//第三季度
start = 7;
} else if ((month >= 10 && month <= 12)) {//第四季度
start = 10;
}
LocalDateTime localDateTime2 = localDateTime.plusMonths(start - month).with(TemporalAdjusters.firstDayOfMonth());
return Date.from(localDateTime2.atZone(ZoneId.systemDefault()).toInstant());
}
/***
* 获取季度的最后一天
* @param date
* @return
*/
public static Date getLastDayOfQuarter(Date date) {
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
int month = localDateTime.getMonth().getValue();//当月
int start = 0;
if (month >= 1 && month <= 3) {//第一季度
start = 3;
} else if (month >= 4 && month <= 6) {//第二季度
start = 6;
} else if (month >= 7 && month <= 9) {//第三季度
start = 9;
} else if ((month >= 10 && month <= 12)) {//第四季度
start = 12;
}
LocalDateTime localDateTime2 = localDateTime.plusMonths(start-month).with(TemporalAdjusters.lastDayOfMonth());
return Date.from(localDateTime2.atZone(ZoneId.systemDefault()).toInstant());
}
/**
* 获取当前时间属于星期几
*
* 每日一袋业务使用
* ---by anpeiyong 2019-12-11
*
* 1:星期一、2:星期二、3:星期三、4:星期四、5:星期五、6:星期六、7:星期日
* @param date
* @return
*/
public static int getCurrentTimeBelongToWhichWeek(Date date) {
int[] weekDays = {7,1,2,3,4,5,6};
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
/**
* 获取当前时间的后多少秒
*/
public static Date getAfterSeconds(int seconds) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, seconds);
return calendar.getTime();
}
public static Date getDateByDateTime(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
// 将时分秒,毫秒域清零
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
}
网友评论