在软件开发过程中,常常会面临各种涉及时间的情境,例如需要获取给定月份的上一个月等情况。因此,我总结了在开发中经常遇到的关于日期的问题,并将它们整合成了一个方便调用的Java工具类。如下:
1、根据传入的日期跨度n,返回当前日期的前n天
public static List<String> nDaysBeforeOfObtain(String step){
LocalDate currentDate = LocalDate.now();
List<String> datesList = new ArrayList<>();
List<LocalDate> previousFiveDays = new ArrayList<>();
//时间跨度
int n = Integer.parseInt(step);
// 逐一计算前n天的日期
for (int i = 1; i < n; i++) {
previousFiveDays.add(currentDate.minusDays(i));
}
// 格式化日期为字符串
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
for (LocalDate date : previousFiveDays) {
String day = date.format(formatter);
datesList.add(day);
}
Collections.reverse(datesList);
return datesList;
}
2、根据年、月获取最后月结束日期
public static String getLastDay(int year, int month) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, 0);
return sdf.format(cal.getTime());
}
3、获取上月开始结束时间
public static List<String> getBEOfLastMonth(){
List<String> list = new ArrayList<>();
LocalDate currentDate = LocalDate.now();
// 获取上一个月的开始时间
LocalDate previousMonthStart = currentDate.minusMonths(1).withDayOfMonth(1);
String beginOfLastMonth = previousMonthStart.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
// 获取上一个月的结束时间
LocalDate previousMonthEnd = previousMonthStart.plusMonths(1);
String endOfLastMonth = previousMonthEnd.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
list.add(beginOfLastMonth);
list.add(endOfLastMonth);
return list;
}
4、获取本月开始结束时间
public static List<String> getBEOfMonth(String month){
// month += "-01";
List<String> list = new ArrayList<>();
// 将字符串转换为 LocalDate 对象
LocalDate firstDayOfMonth = LocalDate.parse(month, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
// 获取月份的开始时间
String startOfMonth = firstDayOfMonth.withDayOfMonth(1).toString();
// 获取月份的结束时间
LocalDate lastDayOfMonth = firstDayOfMonth.withDayOfMonth(firstDayOfMonth.lengthOfMonth());
String endOfMonth = lastDayOfMonth.toString();
list.add(startOfMonth);
list.add(endOfMonth);
return list;
}
5、获取下月开始结束时间
public static List<String> getBEOfNextMonth(String month){
month += "-01";
List<String> list = new ArrayList<>();
// 将字符串转换为 LocalDate 对象
LocalDate firstDayOfMonth = LocalDate.parse(month, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
LocalDate nextDate = firstDayOfMonth.plusMonths(1);
String startOfNextMonth = nextDate.toString();
// 获取月份的结束时间
LocalDate lastDayOfMonth = nextDate.withDayOfMonth(nextDate.lengthOfMonth());
String endOfNextMonth = lastDayOfMonth.toString();
list.add(startOfNextMonth);
list.add(endOfNextMonth);
return list;
}
6、获取传入日期所在月全部日期集合
public static List<String> getMonthFullDay(String date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
List<String> fullDayList = new ArrayList<String>();
int year = Integer.parseInt(date.substring(0, 4));
int month = Integer.parseInt(date.substring(5, 7));
int day = 1;// 所有月份从1号开始
Calendar cal = Calendar.getInstance();// 获得当前日期对象
cal.clear();// 清除信息
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);// 1月从0开始
cal.set(Calendar.DAY_OF_MONTH, day);
int count = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int j = 0; j <= (count - 1); ) {
if (sdf.format(cal.getTime()).equals(getLastDay(year, month)))
break;
cal.add(Calendar.DAY_OF_MONTH, j == 0 ? +0 : +1);
j++;
fullDayList.add(sdf.format(cal.getTime()));
}
return fullDayList;
}
7、获取传入日期所在周全部日期集合
public static List<String> getAllDateOfWeek(String pdate){
DateTimeFormatter dft = DateTimeFormatter.ofPattern("yyyy-MM-dd");
List<String> list = new ArrayList<>();
LocalDate date = LocalDate.parse(pdate,dft);
LocalDate startOfWeek = date.with(DayOfWeek.MONDAY); // 本周的周一
LocalDate endOfWeek = startOfWeek.plusDays(6); // 本周的周日
LocalDate tempDate = startOfWeek;
while (!tempDate.isAfter(endOfWeek)) {
list.add(tempDate.format(dft));
tempDate = tempDate.plusDays(1);
}
return list;
}
8、判断日期列表中的日期是否在同一个月内
public static boolean checkSameMonth(List<String> dates) {
if (dates.isEmpty()) {
return false;
}
DateTimeFormatter dft = DateTimeFormatter.ofPattern("yyyy-MM-dd");
List<LocalDate> dateList = new ArrayList<>();
for (String date : dates) {
dateList.add(LocalDate.parse(date,dft));
}
int month = dateList.get(0).getMonthValue();
for (LocalDate date : dateList) {
if (date.getMonthValue() != month) {
return false;
}
}
return true;
}
9、根据传入日期获取当前日期全部月开始和结束时间(当前年),当前日期所对应月开始时间和当前时间
public static List<List<String>> getAllBEOfMonth(String date) {
List<List<String>> monthDates = new ArrayList<>();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate currentDate = LocalDate.parse(date, formatter);
int year = currentDate.getYear();
Integer endMonth = Integer.parseInt(date.substring(5,7));
for (int month = 1; month <= endMonth; month++) {
List<String> beOfMonth = new ArrayList<>();
YearMonth yearMonth = YearMonth.of(year, month);
LocalDate startOfMonth = yearMonth.atDay(1);
LocalDate endOfMonth = yearMonth.atEndOfMonth();
beOfMonth.add(startOfMonth.format(formatter));
beOfMonth.add(endOfMonth.format(formatter));
monthDates.add(beOfMonth);
}
monthDates.get(monthDates.size()-1).set(1,date);
return monthDates;
}
10、根据时间间隔(单位为分钟)划分日期
public static List<String> splitByStep(int step) {
// 初始化结果列表
List<String> result = new ArrayList<>();
// 定义时间格式化器
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
// 定义起始时间
LocalTime time = LocalTime.of(0, 0);
// 循环遍历一天中的每一分钟,并根据步长添加时间点到结果列表中
while (!time.equals(LocalTime.of(0, 0).minusMinutes(step))) {
// 格式化时间,并添加到结果列表中
result.add(time.format(formatter));
// 将时间增加步长分钟
time = time.plusMinutes(step);
}
// 添加最后一个时间点 "24:00"
result.add("24:00");
return result;
}
11、获取一段时间的天数
public static Long getNumOfRangeDate(String start,String end){
DateTimeFormatter dft = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate startDate = LocalDate.parse(start, dft);
LocalDate endDate = LocalDate.parse(end, dft);
return ChronoUnit.DAYS.between(startDate, endDate)+1;
}