package com.dc.galaxydata.model; import com.dc.common.util.DateUtil; import java.util.Calendar; import java.util.Date; public class DateLastEndUtil { public static void main(String[] args) { // System.out.println(DateUtil.format(lastMonthEnd(new Date()), DateUtil.YMD_STANDARD_FORMAT)); // System.out.println(DateUtil.format(lastQueryEnd(new Date()), DateUtil.YMD_STANDARD_FORMAT)); // System.out.println(DateUtil.format(lastYearEnd(new Date()), DateUtil.YMD_STANDARD_FORMAT)); // System.out.println(DateUtil.format(monthEnd(new Date()), DateUtil.YMD_STANDARD_FORMAT)); // System.out.println(DateUtil.format(queryEnd(new Date()), DateUtil.YMD_STANDARD_FORMAT)); // System.out.println(DateUtil.format(yearEnd(new Date()), DateUtil.YMD_STANDARD_FORMAT)); // System.out.println(DateUtil.format(getMonthEnd(DateUtil.parse("2024-05-30", DateUtil.YMD_STANDARD_FORMAT)), DateUtil.YMD_STANDARD_FORMAT)); // System.out.println(DateUtil.format(getQuarterEnd(DateUtil.parse("2024-05-30", DateUtil.YMD_STANDARD_FORMAT)), DateUtil.YMD_STANDARD_FORMAT)); // System.out.println(DateUtil.format(getYearEnd(DateUtil.parse("2024-05-30", DateUtil.YMD_STANDARD_FORMAT)), DateUtil.YMD_STANDARD_FORMAT)); // System.out.println(DateUtil.format(getPreMonthEnd(DateUtil.parse("2024-05-30", DateUtil.YMD_STANDARD_FORMAT)), DateUtil.YMD_STANDARD_FORMAT)); // System.out.println(DateUtil.format(getPreQuarterEnd(DateUtil.parse("2024-05-30", DateUtil.YMD_STANDARD_FORMAT)), DateUtil.YMD_STANDARD_FORMAT)); // System.out.println(DateUtil.format(getPreYearEnd(DateUtil.parse("2024-05-30", DateUtil.YMD_STANDARD_FORMAT)), DateUtil.YMD_STANDARD_FORMAT)); System.out.println(getSameYearFlag(DateUtil.parse("2023-05-30", DateUtil.YMD_STANDARD_FORMAT),DateUtil.parse("2024-05-30", DateUtil.YMD_STANDARD_FORMAT))); } public static Boolean getSameMonthFlag(Date beginDate, Date endDate) { Calendar beginCalendar = Calendar.getInstance(); beginCalendar.setTime(beginDate); Calendar endCalendar = Calendar.getInstance(); endCalendar.setTime(endDate); return beginCalendar.get(Calendar.YEAR) == endCalendar.get(Calendar.YEAR) && beginCalendar.get(Calendar.MONTH) == endCalendar.get(Calendar.MONTH); } public static Boolean getSameQuarterFlag(Date beginDate, Date endDate) { Calendar beginCalendar = Calendar.getInstance(); beginCalendar.setTime(beginDate); Calendar endCalendar = Calendar.getInstance(); endCalendar.setTime(endDate); return beginCalendar.get(Calendar.YEAR) == endCalendar.get(Calendar.YEAR) && beginCalendar.get(Calendar.MONTH) / 3 + 1 == endCalendar.get(Calendar.MONTH) / 3 + 1; } public static Boolean getSameYearFlag(Date beginDate, Date endDate) { Calendar beginCalendar = Calendar.getInstance(); beginCalendar.setTime(beginDate); Calendar endCalendar = Calendar.getInstance(); endCalendar.setTime(endDate); return beginCalendar.get(Calendar.YEAR) == endCalendar.get(Calendar.YEAR); } public static Date getMonthEnd(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.add(Calendar.MONTH, 1); calendar.add(Calendar.DATE, -1); return calendar.getTime(); } public static Date getQuarterEnd(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); // 设置到当前季度的第一天 calendar.set(Calendar.DAY_OF_MONTH, 1); // 根据月份移动到上一个季度的最后一天 switch (calendar.get(Calendar.MONTH)) { case Calendar.JANUARY: case Calendar.FEBRUARY: case Calendar.MARCH: // 第一季度的最后一天是3月的最后一天 calendar.set(Calendar.MONTH, Calendar.MARCH); break; case Calendar.APRIL: case Calendar.MAY: case Calendar.JUNE: // 第二季度的最后一天是6月的最后一天 calendar.set(Calendar.MONTH, Calendar.JUNE); break; case Calendar.JULY: case Calendar.AUGUST: case Calendar.SEPTEMBER: // 第三季度的最后一天是9月的最后一天 calendar.set(Calendar.MONTH, Calendar.SEPTEMBER); break; case Calendar.OCTOBER: case Calendar.NOVEMBER: case Calendar.DECEMBER: // 第四季度的最后一天是12月的最后一天 calendar.set(Calendar.MONTH, Calendar.DECEMBER); break; } // 设置到季度的最后一天 calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); return calendar.getTime(); } public static Date getYearEnd(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int currentYear = calendar.get(Calendar.YEAR); calendar.set(currentYear, Calendar.JANUARY, 1); calendar.add(Calendar.YEAR, 1); calendar.add(Calendar.DATE, -1); return calendar.getTime(); } public static Date getPreMonthEnd(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.add(Calendar.DATE, -1); return calendar.getTime(); } public static Date getPreQuarterEnd(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int currentQuarter = calendar.get(Calendar.MONTH) / 3; calendar.set(Calendar.MONTH, currentQuarter * 3); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.add(Calendar.DAY_OF_MONTH, -1); return calendar.getTime(); } public static Date getPreYearEnd(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int currentYear = calendar.get(Calendar.YEAR); calendar.set(currentYear, Calendar.JANUARY, 1); calendar.add(Calendar.DATE, -1); return calendar.getTime(); } }
标签:季末,DateUtil,同一,FORMAT,月末,YMD,Date,Calendar,calendar From: https://www.cnblogs.com/yxl-wyb/p/18279506