需要为项目写一些utils处理日期
这份使用说明书旨在介绍如何使用 DateUtils
和 CalendarUtil
类来执行日期处理操作。
日期工具类使用说明书
简介
本文档介绍了如何使用日期工具类(DateUtils)和日历工具类(CalendarUtil)来处理日期和时间相关的操作。这两个工具类可以帮助你执行各种日期计算和转换操作,例如获取季度末日期、获取年份的开始和结束日期、获取月份的开始和结束日期等。
日期工具类(DateUtils)
获取年份
获取年份的第一天和最后一天
获取月份的第一天和最后一天
获取一周的第一天和最后一天
获取季度的第一天和最后一天
字符串日期转换为Date对象
日历工具类(CalendarUtil)
获取每个季度末的日期
获取指定年份的开始和结束日期
获取指定月份的开始和结束日期
获取指定季度的开始和结束日期
获取最近的期末日期
获取一年前的日期
获取一周的开始和结束日期
获取任意时间段的开始和结束日期
DateUtils
类包含了各种用于日期处理的方法。
package com.wego.training.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class DateUtils {
public static int getYearByDate(String dateStr) {
return getDateComponent(dateStr, Calendar.YEAR);
}
public static String getFirstDayofYear(String dateStr, String format) {
return getFirstOrLastDayofYear(dateStr, format, true);
}
public static String getLastDayofYear(String dateStr, String format) {
return getFirstOrLastDayofYear(dateStr, format, false);
}
public static String getFirstDayofMonth(String dateStr, String format) {
return getFirstOrLastDayofMonth(dateStr, format, true);
}
public static String getLastDayofMonth(String dateStr, String format) {
return getFirstOrLastDayofMonth(dateStr, format, false);
}
public static String getFirstDayOfWeek(String dateStr, int firstDayOfWeek, String format) {
return getFirstOrLastDayOfWeek(dateStr, firstDayOfWeek, format, true);
}
public static String getLastDayOfWeek(String dateStr, int firstDayOfWeek, String format) {
return getFirstOrLastDayOfWeek(dateStr, firstDayOfWeek, format, false);
}
public static String getFirstDateOfSeason(String dateStr, String format) {
return getDateOfSeason(dateStr, format, true);
}
public static String getLastDateOfSeason(String dateStr, String format) {
return getDateOfSeason(dateStr, format, false);
}
public static Date stringToDate(String dateStr, String format) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.parse(dateStr);
}
private static int getDateComponent(String dateStr, int component) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = sdf.parse(dateStr);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(component);
} catch (ParseException e) {
e.printStackTrace();
return -1;
}
}
private static String getFirstOrLastDayofYear(String dateStr, String format, boolean isFirst) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = sdf.parse(dateStr);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.MONTH, isFirst ? 0 : 11);
calendar.set(Calendar.DAY_OF_MONTH, isFirst ? 1 : 31);
return new SimpleDateFormat(format).format(calendar.getTime());
} catch (ParseException e) {
e.printStackTrace();
return "";
}
}
private static String getFirstOrLastDayofMonth(String dateStr, String format, boolean isFirst) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = sdf.parse(dateStr);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, isFirst ? 1 : calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return new SimpleDateFormat(format).format(calendar.getTime());
} catch (ParseException e) {
e.printStackTrace();
return "";
}
}
private static String getFirstOrLastDayOfWeek(String dateStr, int firstDayOfWeek, String format, boolean isFirst) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = sdf.parse(dateStr);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.setFirstDayOfWeek(firstDayOfWeek);
calendar.set(Calendar.DAY_OF_WEEK, isFirst ? firstDayOfWeek : (firstDayOfWeek + 6) % 7);
return new SimpleDateFormat(format).format(calendar.getTime());
} catch (ParseException e) {
e.printStackTrace();
return "";
}
}
private static String getDateOfSeason(String dateStr, String format, boolean isFirst) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = sdf.parse(dateStr);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int month = calendar.get(Calendar.MONTH);
int quarterStartMonth = (month / 3) * 3;
calendar.set(Calendar.MONTH, isFirst ? quarterStartMonth : (quarterStartMonth + 2));
calendar.set(Calendar.DAY_OF_MONTH, isFirst ? 1 : calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return new SimpleDateFormat(format).format(calendar.getTime());
} catch (ParseException e) {
e.printStackTrace();
return "";
}
}
public static void main(String[] args) {
// Test DateUtils methods
String dateStr = "2023-09-30";
System.out.println("Year: " + DateUtils.getYearByDate(dateStr));
System.out.println("First Day of Year: " + DateUtils.getFirstDayofYear(dateStr, "yyyy-MM-dd"));
System.out.println("Last Day of Year: " + DateUtils.getLastDayofYear(dateStr, "yyyy-MM-dd"));
System.out.println("First Day of Month: " + DateUtils.getFirstDayofMonth(dateStr, "yyyy-MM-dd"));
System.out.println("Last Day of Month: " + DateUtils.getLastDayofMonth(dateStr, "yyyy-MM-dd"));
System.out.println("First Day of Week (Sunday): " + DateUtils.getFirstDayOfWeek(dateStr, Calendar.SUNDAY, "yyyy-MM-dd"));
System.out.println("Last Day of Week (Sunday): " + DateUtils.getLastDayOfWeek(dateStr, Calendar.SUNDAY, "yyyy-MM-dd"));
System.out.println("First Day of Week (Monday): " + DateUtils.getFirstDayOfWeek(dateStr, Calendar.MONDAY, "yyyy-MM-dd"));
System.out.println("Last Day of Week (Monday): " + DateUtils.getLastDayOfWeek(dateStr, Calendar.MONDAY, "yyyy-MM-dd"));
System.out.println("First Date of Season: " + DateUtils.getFirstDateOfSeason(dateStr, "yyyy-MM-dd"));
System.out.println("Last Date of Season: " + DateUtils.getLastDateOfSeason(dateStr, "yyyy-MM-dd"));
// Test CalendarUtil methods
try {
String beginDate = "2023-01-01";
String endDate = "2023-12-31";
List quarterEndDates = CalendarUtil.getQuarterLastDateByRangeDate(beginDate, endDate);
System.out.println("Quarter End Dates between " + beginDate + " and " + endDate + ": " + quarterEndDates);
String year = "2023";
Map yearDates = CalendarUtil.getBeginAndEndDateByYear(year);
System.out.println("Year " + year + " Start and End Dates: " + yearDates);
String month = "2023-09";
Map monthDates = CalendarUtil.getBeginAndEndDateByMonth(month);
System.out.println("Month " + month + " Start and End Dates: " + monthDates);
String season = "2023年3季度";
Map seasonDates = CalendarUtil.getBeginAndEndDateBySeason(season);
System.out.println("Season " + season + " Start and End Dates: " + seasonDates);
String lastOfMonth = CalendarUtil.getLastOfMonth("2023-09-30");
System.out.println("Last Day of Previous Month: " + lastOfMonth);
String oneYearBefore = CalendarUtil.getOneYearBeforeDate("2023-09-30");
System.out.println("One Year Before Date: " + oneYearBefore);
String weekDates = "2023-09-24,2023-09-30";
Map weekDateMap = CalendarUtil.getBeginAndEndDateByAnyTIME(weekDates);
System.out.println("Start and End Dates from Week Dates: " + weekDateMap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.wego.training.util;
import com.alibaba.excel.util.StringUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class CalendarUtil {
private static final String DATE_FORMAT = "yyyy-MM-dd";
public static List getQuarterLastDateByRangeDate(String beginDate, String endDate) throws ParseException {
String[] quarterEnds = {"03-31", "06-30", "09-30", "12-31"};
List dateStr = new ArrayList<>();
int beginYear = DateUtils.getYearByDate(beginDate);
int endYear = DateUtils.getYearByDate(endDate);
long beginLong = DateUtils.stringToDate(beginDate, DATE_FORMAT).getTime();
long endLong = DateUtils.stringToDate(endDate, DATE_FORMAT).getTime();
do {
for (String quarterEnd : quarterEnds) {
String quarterEndDate = beginYear + "-" + quarterEnd;
long quarterLong = DateUtils.stringToDate(quarterEndDate, DATE_FORMAT).getTime();
if (beginLong <= quarterLong && endLong >= quarterLong) {
dateStr.add(quarterEndDate);
}
}
beginYear++;
} while (beginYear <= endYear);
return dateStr;
}
public static Map getBeginAndEndDateByYear(String year) {
Map map = new HashMap<>();
if (StringUtils.isNotBlank(year)) {
String date = year + "-01-01";
map.put("begin", DateUtils.getFirstDayofYear(date, DATE_FORMAT));
map.put("end", DateUtils.getLastDayofYear(date, DATE_FORMAT));
}
return map;
}
public static Map getBeginAndEndDateByMonth(String month) {
Map map = new HashMap<>();
if (StringUtils.isNotBlank(month)) {
String date = month + "-01";
map.put("begin", DateUtils.getFirstDayofMonth(date, DATE_FORMAT));
map.put("end", DateUtils.getLastDayofMonth(date, DATE_FORMAT));
}
return map;
}
public static Map getBeginAndEndDateBySeason(String season) throws Exception {
String[] quarterEnds = {"03-31", "06-30", "09-30", "12-31"};
Map map = new HashMap<>();
if (StringUtils.isNotBlank(season)) {
String year = season.substring(0, 4);
String numSeason = season.substring(5, 6);
int num = Integer.parseInt(numSeason);
if (num < 1 || num > 4) {
throw new Exception("不存在第" + num + "季度");
}
String date = year + "-" + quarterEnds[num - 1];
map.put("begin", DateUtils.getFirstDateOfSeason(date, DATE_FORMAT));
map.put("end", DateUtils.getLastDateOfSeason(date, DATE_FORMAT));
}
return map;
}
public static String getLastOfMonth(String date) throws ParseException {
if (StringUtils.isBlank(date)) {
return date;
}
String lastDayofMonth = DateUtils.getLastDayofMonth(date, DATE_FORMAT);
if (date.equals(lastDayofMonth)) {
return lastDayofMonth;
}
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
c.setTime(sdf.parse(date));
c.add(Calendar.MONTH, -1);
c.set(Calendar.DATE, c.getActualMaximum(Calendar.DATE));
Date lastDateOfPrevMonth = c.getTime();
return sdf.format(lastDateOfPrevMonth);
}
public static String getOneYearBeforeDate(String date) throws ParseException {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
calendar.setTime(sdf.parse(date));
calendar.add(Calendar.YEAR, -1);
calendar.add(Calendar.DATE, 1);
return sdf.format(calendar.getTime());
}
public static Map getBeginAndEndDateByAnyTIME(String date) {
Map map = new HashMap<>();
if (StringUtils.isNotBlank(date)) {
String[] dateArray = date.split(",");
map.put("begin", dateArray[0]);
map.put("end", dateArray[1]);
}
return map;
}
}
Year: 2023
First Day of Year: 2023-01-01
Last Day of Year: 2023-12-31
First Day of Month: 2023-09-01
Last Day of Month: 2023-09-30
First Day of Week (Sunday): 2023-09-24
Last Day of Week (Sunday): 2023-09-30
First Day of Week (Monday): 2023-09-25
Last Day of Week (Monday): 2023-10-01
First Date of Season: 2023-07-01
Last Date of Season: 2023-09-30
Quarter End Dates between 2023-01-01 and 2023-12-31: [2023-03-31, 2023-06-30, 2023-09-30, 2023-12-31]
Year 2023 Start and End Dates: {end=2023-12-31, begin=2023-01-01}
Month 2023-09 Start and End Dates: {end=2023-09-30, begin=2023-09-01}
Season 2023年3季度 Start and End Dates: {end=2023-09-30, begin=2023-07-01}
Last Day of Previous Month: 2023-09-30
One Year Before Date: 2022-10-01
Start and End Dates from Week Dates: {end=2023-09-30, begin=2023-09-24}