Java日期时间处理实用技术整理
计算工作日(考虑节假日调休)
好文参考:https://juejin.cn/post/7319308611150643250
好文参考:java 获取n个工作日后的日期(包含法定节假日、双休日、节后补班) - 简书
好文参考:java计算工作日(包含法定节假日和调休)_java获取工作日含法定节假日-CSDN博客
计算工作日的天数(只考虑非周末)
业务场景:计算某个日期或者当前时间往后的n个工作日的具体日期是什么时候,包括时分秒,这里的工作日是指的只要不是周末,那就是工作日,不考虑节假日和调休,例如2024/6/28 13:19:30是周五,那么三个工作日后是2024/7/3 13:19:30
/**
* 计算指定开始时间的n个工作日后的具体日期【包括时分秒】
* @param n n个工作日后
* @param startDate 开始时间
* @return
*/
public Date calculateWorkdayAfterNDaysWithTime(int n, LocalDateTime startDate) {
if (startDate == null) {
startDate = LocalDateTime.now(); // 如果没有提供日期时间,默认为当前日期时间
}
LocalDate currentDate = startDate.toLocalDate();
int workdaysToAdd = n; // 还需要添加的工作日数
while (workdaysToAdd > 0) {
currentDate = currentDate.plusDays(1); // 增加一天
DayOfWeek dayOfWeek = currentDate.getDayOfWeek();
// 如果增加的这一天不是周末,就减少一个工作日数
if (!dayOfWeek.equals(DayOfWeek.SATURDAY) && !dayOfWeek.equals(DayOfWeek.SUNDAY)) {
workdaysToAdd--;
}
}
// 将计算得到的工作日后的日期与原始时间的时分秒结合
LocalDateTime localDateTime = currentDate.atTime(startDate.toLocalTime());
// 将LocalDateTime转换为ZonedDateTime,以便转换为旧版的Date对象
ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
return Date.from(zonedDateTime.toInstant());
}
使用calculateWorkdayAfterNDaysWithTime方法,如果是从当前时间开始算,那么第二个参数传入null,否则传入指定的开始日期,注意类型是LocalDateTime
calculateWorkdayAfterNDaysWithTime(3, null); //计算当前时间的3个工作日后的日期
计算给定日期字符串与当前日期之间的天数差
/**
* 计算给定日期字符串与当前日期之间的天数差。
*
* @param dateString 给定的日期字符串,格式为"yyyy-MM-dd HH:mm:ss"
* @return 天数差,如果给定日期在当前日期之前,则为负数
*/
public static long daysBetweenNowAnd(String dateString) {
// 定义日期格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 将字符串转换为LocalDateTime
LocalDateTime targetDateTime = LocalDateTime.parse(dateString, formatter);
// 转换为LocalDate以忽略时间部分
LocalDate targetDate = targetDateTime.toLocalDate();
// 获取当前日期
LocalDate currentDate = LocalDate.now();
// 计算两个日期之间的天数差
return ChronoUnit.DAYS.between(targetDate, currentDate);
}
格式化日期
日期格式转换【20240530 -->5月30日】
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("M月dd日");
LocalDate statisticsTime = LocalDate.parse("20240530", inputFormatter);
String monAndDayTime = statisticsTime.format(outputFormatter);
骚戴理解:不管转成什么格式,首先需要转成LocalDate才行
日期减少两天
需求是把数据库中20240530这样格式的字符串减少两天后转成2024年5月30日格式的字符串
// 日期格式转换【20240530 -->2024年5月30日】
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("M月dd日");
LocalDate statisticsTime = LocalDate.parse("数据库中字符串20240530", inputFormatter);
String monAndDayTime = statisticsTime.format(outputFormatter);
// 日期减少两天【20240530 -->2024年5月28日】
LocalDate adjustedDate = statisticsTime.minusDays(2);
String nearlyTwoDays = adjustedDate.format(outputFormatter);
骚戴理解:需要注意的是LocalDate.parse的第一个参数要传入字符串类型
月份减少两月
LocalDate statisticTime = LocalDate.parse("数据库中字符串20240530", inputFormatter);
int i = statisticTime.getMonthValue()-1;
骚戴理解:需要注意的是LocalDate.parse的第一个参数要传入字符串类型
Date转String
// 创建一个Date对象
Date date = new Date();
// 创建一个SimpleDateFormat对象,指定日期/时间格式
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 使用format方法将Date转换为字符串
String dateString = formatter.format(date);
String转Date
String time = "20241024040712";
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = formatter.parse(time);
格式化日期【2024年4月25日(星期四) 下午04:34】
// 格式化日期【2024年4月25日(星期四) 下午04:34】
SimpleDateFormat formatter = new SimpleDateFormat("yyyy年M月d日(EEEE) ahh:mm", Locale.CHINA);
String formattedDate = formatter.format(conference.getTimeStart());
格式化日期【yyyy年M月d日】
SimpleDateFormat formatter = new SimpleDateFormat("yyyy年M月d日");
String statisticTimeStr = formatter.format(xxx); //xxx可以是object、Date类型
获取月份
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class ExtractMonthJava8Example {
public static void main(String[] args) {
String dateString = "2024-05-24";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
try {
LocalDate date = LocalDate.parse(dateString, formatter);
int month = date.getMonthValue();
System.out.println("月份: " + month);
} catch (Exception e) {
e.printStackTrace();
}
}
}