java判断申请时间是否晚于请假时间的下周一
问题是:要求请假条的申请时间不得晚于请假时间的下周一。
上一篇中写到了相对于这个问题的前端js的解决方案,这一篇主要就是对应的java解决方案,废话不多说,下面放代码
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TestMain {
public static boolean isApplyDateBeforeNextMonday(String applyDateString, String startDateString) {
try{
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date applyDate = formatter.parse(applyDateString);
Date startDate = formatter.parse(startDateString);
System.out.println("申请时间----->"+applyDate+"请假时间------->"+startDate);
// 获取请假时间所在周的下一个周一
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == Calendar.SUNDAY) {
// 如果是周日,则下一个周一就是当天加上一天
calendar.add(Calendar.DAY_OF_MONTH, 1);
} else {
// 否则,计算距离下一个周一的天数并加上
int diff = Calendar.MONDAY - dayOfWeek;
if (diff <= 0) {
diff += 7; // 如果diff是负数,表示需要加上一个星期的天数
}
calendar.add(Calendar.DAY_OF_MONTH, diff);
}
Date nextMonday = calendar.getTime();
System.out.println("下周一时间------>"+nextMonday);
// 比较申请时间是否在请假时间的下一个周一之前(包括下周一)
if (applyDate.after(startDate) || applyDate.equals(startDate)) {
if (applyDate.before(nextMonday) || applyDate.equals(nextMonday)) {
return true;
}
}
if (applyDate.before(startDate) && (startDate.before(nextMonday) || startDate.equals(nextMonday))) {
return true;
}
}catch(ParseException e){
e.printStackTrace();
}
return false;
}
public static void main(String[] args) {
String applyDateString = "2024-06-03"; // 申请时间
String startDateString = "2024-05-27"; // 请假时间
boolean result = isApplyDateBeforeNextMonday(applyDateString, startDateString);
System.out.println("申请时间是否在请假时间的下一个周一之前(包括下周一): " + result);
}
}
由于我们环境的要求,用的jdk版本是1.6。但是如果你们的环境没有强制要求用1.6版本的话。我觉得用1.8是最好的,方便简单,而且1.8引入了更好的时间处理类
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class TestMain {
public static boolean isApplyDateBeforeNextMonday(String applyDateString, String startDateString) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate applyDate = LocalDate.parse(applyDateString, formatter);
LocalDate startDate = LocalDate.parse(startDateString, formatter);
System.out.println("申请时间----->" + applyDate + " 请假时间------->" + startDate);
// 获取启程时间所在周的下一个周一
LocalDate nextMonday = getNextMonday(startDate);
System.out.println("下周一时间------>" + nextMonday);
// 比较申请时间是否在请假时间的下一个周一之前(包括下周一)
return (!applyDate.isAfter(nextMonday));
}
private static LocalDate getNextMonday(LocalDate date) {
int dayOfWeek = date.getDayOfWeek().getValue();
int daysToAdd = 1; // 至少加1天,因为不可能是当天
if (dayOfWeek != DayOfWeek.MONDAY.getValue()) {
daysToAdd += (DayOfWeek.MONDAY.getValue() - dayOfWeek);
if (daysToAdd > 0) {
// 如果daysToAdd大于0,表示是下周的周一
} else {
// 如果daysToAdd小于等于0,表示是本周的周一或更前面的日期,需要加上一周
daysToAdd += 7;
}
}
return date.plusDays(daysToAdd);
}
public static void main(String[] args) {
// 测试代码
String applyDateString = "2023-07-18";
String startDateString = "2023-07-15";
boolean result = isApplyDateBeforeNextMonday(applyDateString, startDateString);
System.out.println("结果: " + result);
}
}
以上就是对于这个问题的两个不同版本的java解决方案,如果你有更好的方法,欢迎在评论区交流沟通~