使用举例:指定一个日期:如2022-08-26,返回两周前的周五,返回2022-08-12
一、工具类
package com.example.demo;
import java.time.DayOfWeek;
import java.time.LocalDate;
public class MyDataTimeUtil {
/**
* 获取前n个月
* @param n 数值
* @param localDate 比较时间
* @return
*/
public static LocalDate getLastMonth(LocalDate localDate, int n) {
return localDate.minusMonths(n);
}
/**
* 获取后n个月
* @param n 数值
* @param localDate 比较时间
* @return
*/
public static LocalDate getNextMonth(LocalDate localDate, int n) {
return localDate.plusMonths(n);
}
/**
* 获取前n个月
* @param n 数值
* @return
*/
public static LocalDate getLastMonth(int n) {
return getLastMonth(LocalDate.now(), n);
}
/**
* 获取后n个月
* @param n 数值
* @return
*/
public static LocalDate getNextMonth(int n) {
return getNextMonth(LocalDate.now(), n);
}
/**
* 获取开始时间的dayOfWeek的日期
* @param localDate 开始时间
* @param dayOfWeek 周几
* @return 日期
*/
public static LocalDate getCurrentWeekMonday(LocalDate localDate,DayOfWeek dayOfWeek) {
int weekValue = localDate.getDayOfWeek().getValue();
int paramWeekValue = dayOfWeek.getValue();
if (weekValue == paramWeekValue) {
return localDate;
}
if(paramWeekValue>weekValue){
return localDate.plusDays(paramWeekValue - weekValue);
}else {
return localDate.minusDays(weekValue - paramWeekValue);
}
}
/**
* 获取当前时间的dayOfWeek的日期
* @param dayOfWeek 周几
* @return 日期
*/
public static LocalDate getCurrentWeekMonday(DayOfWeek dayOfWeek) {
return getCurrentWeekMonday(LocalDate.now(),dayOfWeek);
}
/**
* 获取开始时间的dayOfWeek的日期
* @param localDate 开始时间
* @param dayOfWeek 周几
* @param n n周前
* @return
*/
public static LocalDate getLastWeekMonday(LocalDate localDate,DayOfWeek dayOfWeek,int n) {
localDate = localDate.minusDays(n*7);
return getCurrentWeekMonday(localDate,dayOfWeek);
}
/**
* 获取当前时间的dayOfWeek的日期
* @param dayOfWeek 周几
* @param n n周前
* @return
*/
public static LocalDate getLastWeekMonday(DayOfWeek dayOfWeek,int n) {
LocalDate localDate = LocalDate.now().minusDays(n*7);
return getCurrentWeekMonday(localDate,dayOfWeek);
}
/**
* 获取开始时间的dayOfWeek的日期
* @param localDate 开始时间
* @param dayOfWeek 周几
* @param n n周后
* @return
*/
public static LocalDate getNextWeekMonday(LocalDate localDate,DayOfWeek dayOfWeek,int n) {
localDate = localDate.plusDays(n*7);
return getCurrentWeekMonday(localDate,dayOfWeek);
}
/**
* 获取开始时间的dayOfWeek的日期
* @param dayOfWeek 周几
* @param n n周后
* @return
*/
public static LocalDate getNextWeekMonday(DayOfWeek dayOfWeek,int n) {
LocalDate localDate = LocalDate.now().plusDays(n*7);
return getCurrentWeekMonday(localDate,dayOfWeek);
}
}
二、测试代码
public static void main(String[] args) {
//日期
LocalDate localDate = LocalDate.of(2022,8,26);
//当前日期 2022-08-26
System.out.println(localDate);
//2022-08-26是那一年 2022
System.out.println(localDate.getYear());
//2022-08-26的月份 8
System.out.println(localDate.getMonth().getValue());
//2022-08-26是本月的第几天 26
System.out.println(localDate.getDayOfMonth());
//2022-08-26是周几 5
System.out.println(localDate.getDayOfWeek().getValue());
//2022-08-26是当年的第多少天 238
System.out.println(localDate.getDayOfYear());
System.out.println("------------");
//获取2022-8-22前一个月 7
System.out.println(MyDataTimeUtil.getLastMonth(localDate,1).getMonthValue());
//获取2022-8-22后一个月 9
System.out.println(MyDataTimeUtil.getNextMonth(localDate,1).getMonthValue());
//获取2022-8-22,所在周的周一 2022-08-22
System.out.println(MyDataTimeUtil.getCurrentWeekMonday(localDate,DayOfWeek.MONDAY));
//获取2022-8-22,两周前的周五 2022-08-12
System.out.println(MyDataTimeUtil.getLastWeekMonday(localDate,DayOfWeek.FRIDAY,2));
//获取2022-8-22,一周后的周一 2022-08-29
System.out.println(MyDataTimeUtil.getNextWeekMonday(localDate,DayOfWeek.MONDAY,1));
}
————————————————
版权声明:本文为CSDN博主「好奇的菜鸟」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_29752857/article/details/126481991