在我们的开发过程中,我们可能会需要对时间进行处理,通常对时间的处理都会比较复杂,这篇文章准备了获取指定时间区间的周频、月频、和季频的java代码,供我们程序开发者参考。
1、java实现:取出开始和结束的时间区间的每周周末时间
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
//初始化两个时间
String startDateString = "20200201";
String endDateString = "20220201";
//将String格式的时间转换为LocalDate对象格式
LocalDate startDate = LocalDate.parse(startDateString, DateTimeFormatter.ofPattern("yyyyMMdd"));
LocalDate endDate = LocalDate.parse(endDateString, DateTimeFormatter.ofPattern("yyyyMMdd"));
List<LocalDate> result = new ArrayList<>();
//取出当前周中的最后一天作为当前时间
LocalDate currentWeekEnd = startDate.with(DayOfWeek.SUNDAY);
//while进行循环,当前时间小于结束时间时进入代码逻辑
while (currentWeekEnd.isBefore(endDate)) {
result.add(currentWeekEnd);
//当前周数加一,在取出周末时间,并加入集合
currentWeekEnd = currentWeekEnd.plusWeeks(1).with(DayOfWeek.SUNDAY);
}
System.out.println(result);
}
}
2、java实现:取出开始和结束的时间区间的每个月月末时间
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
//初始化两个时间
String startDateString = "20200201";
String endDateString = "20220201";
//将string格式的时间转换为LocalDate对象格式
LocalDate startDate = LocalDate.parse(startDateString, DateTimeFormatter.ofPattern("yyyyMMdd"));
LocalDate endDate = LocalDate.parse(endDateString, DateTimeFormatter.ofPattern("yyyyMMdd"));
List<LocalDate> result = new ArrayList<>();
//取出当前月份中的最后一天作为当前时间,并加入到集合中
LocalDate currentDate = startDate.with(TemporalAdjusters.lastDayOfMonth());
result.add(currentDate);
//while循环,当前时间小于结束时间进入代码逻辑
while (currentDate.isBefore(endDate)) {
//当前时间月份加一,在进行月份的最后一天取值,并加入集合
currentDate = currentDate.plusMonths(1).with(TemporalAdjusters.lastDayOfMonth());
result.add(currentDate);
}
System.out.println(result);
}
}
3、java实现:取出开始和结束的时间区间的每个季度季末时间
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
//初始化两个时间
String startDateString = "20200201";
String endDateString = "20220201";
//将String格式的时间转换为LocalDate对象格式
LocalDate startDate = LocalDate.parse(startDateString, DateTimeFormatter.ofPattern("yyyyMMdd"));
LocalDate endDate = LocalDate.parse(endDateString, DateTimeFormatter.ofPattern("yyyyMMdd"));
List<LocalDate> result = new ArrayList<>();
//取出当前时间的月份的最后一天
LocalDate currentQuarterEnd = startDate.with(TemporalAdjusters.lastDayOfMonth());
//while循环,当前月份小于结束时间时进行循环
while (currentQuarterEnd.isBefore(endDate)) {
//if语句进行判断,如果当前月份为3、6、9、12月,那么将获取到的当前月份的最后一天对象加入list集合中
if (currentQuarterEnd.getMonth() == Month.FEBRUARY || currentQuarterEnd.getMonth() == Month.MAY ||
currentQuarterEnd.getMonth() == Month.AUGUST || currentQuarterEnd.getMonth() == Month.NOVEMBER) {
result.add(currentQuarterEnd);
}
//然后当前时间的月份数进行加一操作
currentQuarterEnd = currentQuarterEnd.plusMonths(1).with(TemporalAdjusters.lastDayOfMonth());
//if语句进行判断,如果当前月份加一的月份为3、6、9、12月,那么将获取到的当前月份的最后一天对象加入list集合中
if (currentQuarterEnd.getMonth() == Month.MARCH || currentQuarterEnd.getMonth() == Month.JUNE ||
currentQuarterEnd.getMonth() == Month.SEPTEMBER || currentQuarterEnd.getMonth() == Month.DECEMBER) {
result.add(currentQuarterEnd);
}
}
System.out.println(result);
}
}