package com.dl.common.utils; import org.apache.ibatis.annotations.Param; import java.util.Calendar; import java.util.Date; public class CalendarUtil { //获取年的开始日期时间 public static Date getYearStartDate(Date date){ Calendar calendar = Calendar.getInstance(); //有参数则是参数年的日期时间,无参则默认当前年的日期时间 if (null == date){calendar.setTime(new Date());}else {calendar.setTime(date);} int year = calendar.get(Calendar.YEAR); calendar.set(year, 0, 1, 0, 0, 0); return calendar.getTime(); } //获取年的结束日期时间 public static Date getYearEndDate(Date date){ Calendar calendar = Calendar.getInstance(); //有参数则是参数年的日期时间,无参则默认当前年的日期时间 if (null == date){calendar.setTime(new Date());}else {calendar.setTime(date);} int year = calendar.get(Calendar.YEAR); calendar.set(year, 11, 31, 23, 59, 59); return calendar.getTime(); } //获取季度开始的日期时间 public static Date getSeasonStartDate (Date date) { Calendar calendar = Calendar.getInstance(); //有参数则是参数的季度日期时间,无参则默认当前季度的日期时间 if (null == date){calendar.setTime(new Date());}else {calendar.setTime(date);} int month = calendar.get(Calendar.MONTH); calendar.set(Calendar.MONTH, month / 3 * 3); calendar.set(Calendar.DATE, 1); int year = calendar.get(Calendar.YEAR); int month1 = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DATE); calendar.set(year,month1,day,0,0,0); return calendar.getTime(); } //获取季度的结束日期时间 public static Date getSeasonEndDate (Date date) { Calendar calendar = Calendar.getInstance(); //有参数则是参数的季度日期时间,无参则默认当前季度的日期时间 if (null == date){calendar.setTime(new Date());}else {calendar.setTime(date);} int month = calendar.get(Calendar.MONTH); calendar.set(Calendar.MONTH, (month + 3) / 3 * 3); calendar.set(Calendar.DATE, 1); int year = calendar.get(Calendar.YEAR); int month1 = calendar.get(Calendar.MONTH); int dates = calendar.get(Calendar.DATE); calendar.set(year,month1,dates,23,59,59); Date date1 = new Date(calendar.getTime().getTime() - 24 * 60 * 60 * 1000); return date1; } //获取本月的开始时间 public static Date getMonthStartDate(Date date){ Calendar calendar = Calendar.getInstance(); if (null == date){calendar.setTime(new Date());}else {calendar.setTime(date);} int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); calendar.set(year, month, 1, 0, 0, 0); return calendar.getTime(); } //获取本月的结束时间 public static Date getMonthEndDate(Date date){ Calendar calendar = Calendar.getInstance(); if (null == date){calendar.setTime(new Date());}else {calendar.setTime(date);} int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); calendar.set(year, month + 1, 1-1, 23, 59, 59); return calendar.getTime(); } //获取周的开始时间 public static Date getWeekStartDate(Date date){ Calendar calendar = Calendar.getInstance(); if (null == date){calendar.setTime(new Date());}else {calendar.setTime(date);} int d = 0; if (Calendar.DAY_OF_WEEK == 1) { d = -6; } else { d = 2 - calendar.get(Calendar.DAY_OF_WEEK); } // 获取本周的第一天 calendar.add(Calendar.DAY_OF_WEEK, d); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); return calendar.getTime(); } //获取周的结束时间 public static Date getWeekEndDate(Date date){ Calendar calendar = Calendar.getInstance(); if (null == date){calendar.setTime(new Date());}else {calendar.setTime(date);} calendar.add(Calendar.DAY_OF_WEEK, 6); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); return calendar.getTime(); } }