加哥本周给大家分享一期怎么用java把全年每个月有几周,本周是几号到几号的工具类。便于大家根据需求获取想要的形式进行改造。话不多说,直接给大家上代码。
package com.techfantasy.common.utils;
import com.techfantasy.common.entity.DateRange;
import org.springframework.util.StringUtils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.WeekFields;
import java.util.*;
/**
* @Author feng
* @Date 2023/8/510:21
* @Version 1.0
* @Param
* 时间分割工具类
*/
public class DateSplitUtil {
private DateSplitUtil() {
throw new IllegalStateException("请通过静态方法调用!");
}
/**
* 切分时间,并且获取切分后的时间段集合
*
* @param type 分割类型:按年、按月、按周、按日
* @param startTime 开始时间
* @param endTime 结束时间
* @return 切分后的时间段集合
*/
public static List<DateRange> splitAndGetByType(int type, LocalDateTime startTime, LocalDateTime endTime) {
if (type == 1) {
// 按年切分
return splitDateRangeByYear1(startTime, endTime);
} else if (type == 2) {
// 按月切分
return splitDateRangeByMonth(startTime, endTime);
} else if (type == 3) {
// 按周切分
return splitDateRangeByWeek(startTime, endTime);
} else if (type == 4) {
//按日切分
return splitDateRangeByDay(startTime, endTime);
} else if (type == 5) {
//一次性
List<DateRange> dateRangeList = new ArrayList<>();
DateRange dateRange = new DateRange();
dateRange.setBegin(startTime);
dateRange.setEnd(endTime);
dateRangeList.add(dateRange);
return dateRangeList;
} else {
return new ArrayList<>();
}
}
/**
* 切分时间,并且获取某一轮的时间对象
*
* @param type 分割类型:按年、按月、按周、按日
* @param turn 时间段(分割后的时间轮数,如将2021-10-01~2022-10-01,按月分割时,就能得到12轮时间段)
* @param startTime 开始时间
* @param endTime 结束时间
* @return 第turn段的时间对象
*/
public static DateRange splitAndGetByTurn(int type, long turn, LocalDateTime startTime, LocalDateTime endTime) {
//按年切分
if (type == 1) {
List<DateRange> dateRangeList = splitDateRangeByYear(startTime, endTime);
return getRangeByTurn(dateRangeList, turn);
} else if (type == 2) {
// 按月切分
List<DateRange> dateRangeList = splitDateRangeByMonth(startTime, endTime);
return getRangeByTurn(dateRangeList, turn);
} else if (type == 3) {
// 按周切分
List<DateRange> dateRangeList = splitDateRangeByWeek(startTime, endTime);
return getRangeByTurn(dateRangeList, turn);
} else if (type == 4) {
//按日切分
List<DateRange> dateRangeList = splitDateRangeByDay(startTime, endTime);
return getRangeByTurn(dateRangeList, turn);
} else if (type == 5) {
//一次性
DateRange dateRange = new DateRange();
dateRange.setBegin(startTime);
dateRange.setEnd(endTime);
return dateRange;
} else {
return new DateRange();
}
}
/**
* 按年分割
*
* @param startTime 开始时间
* @param endTime 结束时间
* @return 分割后的时间段集合
*/
private static List<DateRange> splitDateRangeByYear(LocalDateTime startTime, LocalDateTime endTime) {
long seconds = startTime.until(endTime, ChronoUnit.SECONDS);
if (seconds <= 0) {
return new ArrayList<>();
}
//轮数
long turnNum = 0;
//分割的时间段集合,使用累加算法
List<DateRange> dateList = new ArrayList<>();
DateRange range = new DateRange();
range.setBegin(startTime);
while (true) {
turnNum++;
//年份相等,不再累加
if (startTime.getYear() == endTime.getYear()) {
range.setEnd(endTime);
range.setTurnNum(turnNum);
dateList.add(range);
break;
}
//将时间调整为该年的第一天 0时 0分 0秒
startTime = LocalDateTime.of(LocalDate.from(startTime.with(TemporalAdjusters.firstDayOfYear()).plusYears(1)), LocalTime.MIN);
LocalDateTime tmpBegin = startTime;
//计算出上一天的最后一秒
LocalDateTime endDate = tmpBegin.minusSeconds(1);
range.setEnd(endDate);
range.setTurnNum(turnNum);
dateList.add(range);
//创建新的时间段
range = new DateRange();
range.setBegin(tmpBegin);
}
return dateList;
}
/**
* 按月分割
*
* @param startTime 开始时间
* @param endTime 结束时间
* @return 分割后的时间段集合
*/
private static List<DateRange> splitDateRangeByMonth(LocalDateTime startTime, LocalDateTime endTime) {
long seconds = startTime.until(endTime, ChronoUnit.SECONDS);
if (seconds <= 0) {
return new ArrayList<>();
}
//轮数
long turnNum = 0;
//分割的时间段集合,使用累加算法
List<DateRange> dateList = new ArrayList<>();
DateRange range = new DateRange();
range.setBegin(startTime);
while (true) {
turnNum++;
startTime = startTime.plusMonths(1);
//大于截止日期时,不再累加
if (startTime.isAfter(endTime)) {
range.setEnd(endTime);
range.setTurnNum(turnNum);
dateList.add(range);
break;
}
//将时间调整为当前月的第一天的 0时 0分 0秒
startTime = LocalDateTime.of(LocalDate.from(startTime.with(TemporalAdjusters.firstDayOfMonth())), LocalTime.MIN);
LocalDateTime tmpBegin = startTime;
//计算出上一天的最后一秒
LocalDateTime endDate = tmpBegin.minusSeconds(1);
range.setEnd(endDate);
range.setTurnNum(turnNum);
dateList.add(range);
//创建新的时间段
range = new DateRange();
range.setBegin(tmpBegin);
}
return dateList;
}
/**
* 按年分割
*
* @param startTime 开始时间
* @param endTime 结束时间
* @return 分割后的时间段集合
*/
private static List<DateRange> splitDateRangeByYear1(LocalDateTime startTime, LocalDateTime endTime) {
long seconds = startTime.until(endTime, ChronoUnit.SECONDS);
if (seconds <= 0) {
return new ArrayList<>();
}
//轮数
long turnNum = 0;
//分割的时间段集合,使用累加算法
List<DateRange> dateList = new ArrayList<>();
DateRange range = new DateRange();
range.setBegin(startTime);
while (true) {
turnNum++;
//年份相等,不再累加
if (range.getBegin().getYear() == endTime.getYear()) {
range.setEnd(endTime);
range.setTurnNum(turnNum);
dateList.add(range);
break;
}
LocalDateTime tmpBegin = range.getBegin();
startTime=tmpBegin;
//计算出上一天的最后一秒
LocalDateTime endDate = tmpBegin.plusYears(1).minusDays(1);
if(endDate.getYear()==endTime.getYear()&&endDate.getMonth()!=endTime.getMonth()&&endDate.getDayOfYear()==endTime.getDayOfYear()){
range.setEnd(endTime);
range.setTurnNum(turnNum);
dateList.add(range);
break;
}
range.setEnd(endDate);
range.setTurnNum(turnNum);
dateList.add(range);
//创建新的时间段
range = new DateRange();
range.setBegin(endDate.plusDays(1));
//如果上一次结束的时间大于当前传入的开始时间跳出
Date inendTime=Date.from(endTime.atZone(ZoneId.systemDefault()).toInstant());
Date newstartTime =Date.from(range.getBegin().atZone(ZoneId.systemDefault()).toInstant());
if(isBeforeDate(newstartTime, inendTime)){
break;
}
}
return dateList;
}
/**
* 按周分割
*
* @param startTime 开始时间
* @param endTime 结束时间
* @return 分割后的时间段集合
*/
private static List<DateRange> splitDateRangeByWeek(LocalDateTime startTime, LocalDateTime endTime) {
long seconds = startTime.until(endTime, ChronoUnit.SECONDS);
if (seconds <= 0) {
return new ArrayList<>();
}
//轮数
long turnNum = 0;
//分割的时间段集合,使用累加算法
List<DateRange> dateList = new ArrayList<>();
DateRange range = new DateRange();
range.setBegin(startTime);
while (true) {
turnNum++;
startTime = startTime.plusWeeks(1);
//大于截止日期时,不再累加
if (startTime.isAfter(endTime)) {
range.setEnd(endTime);
range.setTurnNum(turnNum);
dateList.add(range);
break;
}
//将时间调整为该周第一天的 0时 0分 0秒
startTime = LocalDateTime.of(LocalDate.from(startTime.with(DayOfWeek.MONDAY)), LocalTime.MIN);
LocalDateTime tmpBegin = startTime;
//计算出上一天的最后一秒
LocalDateTime endDate = tmpBegin.minusSeconds(1);
range.setEnd(endDate);
range.setTurnNum(turnNum);
dateList.add(range);
//创建新的时间段
range = new DateRange();
range.setBegin(tmpBegin);
}
return dateList;
}
/**
* 按日分割
*
* @param startTime 开始时间
* @param endTime 结束时间
* @return 分割后的时间段集合
*/
private static List<DateRange> splitDateRangeByDay(LocalDateTime startTime, LocalDateTime endTime) {
long seconds = startTime.until(endTime, ChronoUnit.SECONDS);
if (seconds < 0) {
return new ArrayList<>();
}
//轮数
long turnNum = 0;
//分割的时间段集合,使用累加算法
List<DateRange> dateList = new ArrayList<>();
DateRange range = new DateRange();
range.setBegin(startTime);
while (true) {
turnNum++;
startTime = startTime.plusDays(1);
//大于截止日期时,不再累加
if (startTime.isAfter(endTime)) {
range.setEnd(endTime);
range.setTurnNum(turnNum);
dateList.add(range);
break;
}
//将时间调整为该天的 0时 0分 0秒
startTime = LocalDateTime.of(startTime.getYear(), startTime.getMonth(), startTime.getDayOfMonth(), 0, 0, 0);
LocalDateTime tmpBegin = startTime;
//计算出上一天的最后一秒
LocalDateTime endDate = tmpBegin.minusSeconds(1);
range.setEnd(endDate);
range.setTurnNum(turnNum);
dateList.add(range);
//创建新的时间段
range = new DateRange();
range.setBegin(tmpBegin);
}
return dateList;
}
/**
* 根据时间段(分割后的时间轮数,2021-10-01~2022-10-01,按月分隔时就有12轮时间段)获取时间范围
*
* @param dateRangeList 分隔后的时间段集合
* @param turn 轮次,当前时间处于第几段
* @return 时间对象
*/
private static DateRange getRangeByTurn(List<DateRange> dateRangeList, long turn) {
DateRange dateRange = new DateRange();
for (DateRange d : dateRangeList) {
if (d.getTurnNum() == turn) {
dateRange = d;
}
}
return dateRange;
}
/**
*判断当前时间是否在[startTime, endTime]区间,注意三个参数的时间格式要一致
** @param nowTime
* * @param startTime
* * @param endTime
* * @return 在时间段内返回true,不在返回false
* **/
public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
if (nowTime.getTime() == startTime.getTime()
|| nowTime.getTime() == endTime.getTime()) {
return true;
}
Calendar date = Calendar.getInstance();
date.setTime(nowTime);
Calendar begin = Calendar.getInstance();
begin.setTime(startTime);
Calendar end = Calendar.getInstance();
end.setTime(endTime);
return date.after(begin) && date.before(end);
}
public static boolean isBeforeDate(Date nowTime,Date endTime) {
if (nowTime.getTime() == endTime.getTime()) {
return true;
}
Calendar date = Calendar.getInstance();
date.setTime(nowTime);
Calendar end = Calendar.getInstance();
end.setTime(endTime);
return date.after(end);
}
/**
* 月底时间处理工具类
*
*/
public static String downdate() throws ParseException {
//1.获取当前时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date now = new Date();
Date nowTime;
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
calendar.set(Calendar.DATE, (calendar.get(Calendar.DATE) + 1));
if (calendar.get(Calendar.DAY_OF_MONTH) == 1) {
//返回本月
Calendar cale = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String now1 = sdf.format(new Date());
cale.setTime(formatter.parse(now1));
cale.add(Calendar.MONTH, 1);
cale.set(Calendar.DAY_OF_MONTH, 0);
String lastDayOfMonth = formatter.format(cale.getTime());
return lastDayOfMonth;
}else{
//返回上月末
Calendar cale = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String now1 = sdf.format(new Date());
cale.setTime(formatter.parse(now1));
cale.add(Calendar.MONTH, 0);
cale.set(Calendar.DAY_OF_MONTH, 0);
String lastDayOfMonth = formatter.format(cale.getTime());
return lastDayOfMonth;
}
//2.判断当前时间是否本月底 是返回本月底 不是返回上月底
}
public static String getChinaDateFromString(String str) {
try {
if(str==null ){
return getChinaCurrentDate();
}
str = str.trim();
int year = 0;
int month = 0;
int day = 0;
//System.out.println("==="+str);
if(str==null || str.equals("null") || str.equals("")){
return getChinaCurrentDate();
}
else if (str.indexOf("年") > 0||str.indexOf("月") > 0||str.indexOf("日") > 0) {
return str;
}
else {
if (str.length() == 10 && (str.indexOf("-") > 0)) {
// by meconsea add str.indexOf("-") > 0
year = Integer.parseInt(str.substring(0, 4));
month = Integer.parseInt(str.substring(5, 7));
day = Integer.parseInt(str.substring(8, 10));
}
else if (str.length() == 8) {
year = Integer.parseInt(str.substring(0, 4));
month = Integer.parseInt(str.substring(4, 6));
day = Integer.parseInt(str.substring(6, 8));
}
else if (str.length() == 6) {
year = Integer.parseInt(str.substring(0, 4));
month = Integer.parseInt(str.substring(4, 6));
}
if (day == 0) {
str = year + "年" + month + "月";
}
else {
str = year + "年" + month + "月" + day + "日";
}
}
}
catch (Exception e) {
e.printStackTrace();
}
return str;
}
public static String getChinaCurrentDate() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
String str_date = dateFormat.format(Calendar.getInstance().getTime());
str_date = str_date.substring(0, 4) + "年" + str_date.substring(4, 6) + "月" +
str_date.substring(6, 8) + "日 ";
return str_date;
}
/***
* 获取系统当前时间
*/
public static String getNowTime(int type) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
String now = sdf.format(new Date());
Date nowTime = sdf.parse(now);
String now1 = sdf1.format(new Date());
Date nowTime1 = sdf1.parse(now);
if (type == 1) {
// yyyy-MM-dd HH:mm:ss
return String.valueOf(now);
} else{
// yyyy-MM-dd
return String.valueOf(now1);
}
}
/***
* 字符串形式日期 补月初神操作 yyyy-MM 转yyyy-MM-dd
* @param dateTime
*/
public static String addDayTime(String dateTime){
String newDateTime =null;
if(dateTime.length()==7){
newDateTime=dateTime+"-01";
}
return newDateTime;
}
/***
* 统一时间格式处理工具类
* 均统一为yyyy-MM-dd形式
* 目前数据中存在的形式有 yyyy-MM-dd yyyy.MM.dd yyyy/MM/dd yyyy.MM (yyyy年MM月)暂时不处理
*/
public static String strToDate(String date) throws ParseException {
String newTime =null;
if (!StringUtils.isEmpty(date)){
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
if (date.contains("-")) {
if(date.length()<=7){
newTime=DateSplitUtil.addDayTime(date);
}else {
newTime=simpleDateFormat1.format(simpleDateFormat1.parse(date));;
}
}
else if (date.contains(".")) {
int count = (date.length()-date.replace(".", "").length());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM");
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy.MM.dd");
if(date.length()<=7 && count == 1){
newTime=simpleDateFormat1.format(simpleDateFormat.parse(date));
}else {
newTime=simpleDateFormat1.format(simpleDateFormat2.parse(date));
}
}
else if (date.contains("/")) {
int count = (date.length()-date.replace("/", "").length());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM");
SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("yyyy/MM/dd");
if(date.length()<=7 && count == 1){
newTime=simpleDateFormat1.format(simpleDateFormat.parse(date));
}else {
newTime=simpleDateFormat1.format(simpleDateFormat3.parse(date));
}
}
}
return newTime;
}
// public static void main(String[] args) {
// int year = 2023; // 指定年份
// List<List<LocalDate>> workWeeks = partitionCalendar(year);
//
// // 输出每个工作周的起始和结束日期
// for (int i = 0; i < workWeeks.size(); i++) {
// List<LocalDate> week = workWeeks.get(i);
// LocalDate start = week.get(0);
// LocalDate end = week.get(week.size() - 1);
// System.out.println("Work Week " + (i + 1) + ": " + start + " - " + end);
// }
// }
public static List<List<LocalDate>> partitionCalendar(int year) {
List<List<LocalDate>> workWeeks = new ArrayList<>();
int dayOfYear = Year.of(year).length(); // 获取指定年份的总天数
LocalDate startDate = LocalDate.of(year, Month.JANUARY, 1);
LocalDate endDate = LocalDate.of(year, Month.DECEMBER, 31);
// 划分工作周
List<LocalDate> week = new ArrayList<>();
for (LocalDate date = startDate; date.isBefore(endDate.plusDays(1)); date = date.plusDays(1)) {
week.add(date);
// 判断是否为工作周的最后一天
if (date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfYear() == dayOfYear) {
workWeeks.add(week);
week = new ArrayList<>();
}
}
return workWeeks;
}
// public static void main(String[] args) {
// int year = 2023; // 指定年份
// int month = 9; // 指定月份
// List<WorkDay> workDays = partitionCalendar(year, month);
//
// // 输出每个工作日是本月的第几周
// DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd");
// for (WorkDay workDay : workDays) {
// LocalDate date = workDay.getDate();
// String week = workDay.getWeek();
// System.out.println(formatter.format(date) + " is in Week " + week);
// }
// }
public static List<WorkDay> partitionCalendar(int year, int month) {
List<WorkDay> workDays = new ArrayList<>();
YearMonth yearMonth = YearMonth.of(year, month);
LocalDate startDate = yearMonth.atDay(1);
LocalDate endDate = yearMonth.atEndOfMonth();
// 划分工作日
for (LocalDate date = startDate; date.isBefore(endDate.plusDays(1)); date = date.plusDays(1)) {
DayOfWeek dayOfWeek = date.getDayOfWeek();
// 判断是否为工作日(周一到周五)
if (dayOfWeek != DayOfWeek.SATURDAY && dayOfWeek != DayOfWeek.SUNDAY) {
int week = date.get(WeekFields.of(DayOfWeek.MONDAY, 1).weekOfMonth()); // 获取是本月的第几周
workDays.add(new WorkDay(date, String.valueOf(week)));
}
}
return workDays;
}
static class WorkDay {
private LocalDate date;
private String week;
public WorkDay(LocalDate date, String week) {
this.date = date;
this.week = week;
}
public LocalDate getDate() {
return date;
}
public String getWeek() {
return week;
}
}
// public static void main(String[] args) {
// int year = 2022; // 指定年份
// int month = 3; // 指定月份
// List<Week> weeklyCalendar = generateWeeklyCalendar(year, month);
//
// // 输出周日历
// DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd");
// for (Week week : weeklyCalendar) {
// System.out.println("Week " + week.getWeekNumber() + ":");
// for (LocalDate date : week.getDates()) {
// System.out.print(formatter.format(date) + " ");
// }
// System.out.println();
// }
// }
public static List<Week> generateWeeklyCalendar(int year, int month) {
List<Week> weeklyCalendar = new ArrayList<>();
YearMonth yearMonth = YearMonth.of(year, month);
LocalDate startDate = yearMonth.atDay(1);
LocalDate endDate = yearMonth.atEndOfMonth();
int weekNumber = 1;
List<LocalDate> dates = new ArrayList<>();
for (LocalDate date = startDate; date.isBefore(endDate.plusDays(1)); date = date.plusDays(1)) {
dates.add(date);
// 如果是周日,则表示一个完整的周结束,创建一个新的周
if (date.getDayOfWeek() == DayOfWeek.SUNDAY) {
weeklyCalendar.add(new Week(weekNumber, new ArrayList<>(dates)));
dates.clear();
weekNumber++;
}
}
// 处理最后一周不满一周的情况
if (!dates.isEmpty()) {
weeklyCalendar.add(new Week(weekNumber, dates));
}
return weeklyCalendar;
}
// static class Week {
// private int weekNumber;
// private List<LocalDate> dates;
//
// public Week(int weekNumber, List<LocalDate> dates) {
// this.weekNumber = weekNumber;
// this.dates = dates;
// }
//
// public int getWeekNumber() {
// return weekNumber;
// }
//
// public List<LocalDate> getDates() {
// return dates;
// }
// }
public static void main(String[] args) {
int year = 2023; // 指定年份
Map<Integer, List<Week>> yearlyCalendar = generateYearlyCalendar(year);
// 输出每个月的周日历
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
for (int month : yearlyCalendar.keySet()) {
System.out.println("Month " + month + ":");
List<Week> weeklyCalendar = yearlyCalendar.get(month);
for (Week week : weeklyCalendar) {
System.out.println(" Week " + week.getWeekNumber() + ":");
//判断weeklyCalendar.get(0).size是否等于7 不等于的话从上一期上不全
// 1.不是1月的话拿上一个月的最后一月的最后一星期的数据顺序追加到最前面
// 2.1月份的话需要拿上一年最后一月最后一星期的数据来补全
//判断weeklyCalendar.get((weeklyCalendar.size))不足七天的话舍弃
int last = weeklyCalendar.size()-1;
// if(weeklyCalendar.get(last).dates.size() < 7){
// weeklyCalendar.remove(last);
// }
for (LocalDate date : week.getDates()) {
System.out.print(" " + formatter.format(date) + " ");
}
System.out.println();
}
}
}
public static Map<Integer, List<Week>> generateYearlyCalendar(int year) {
Map<Integer, List<Week>> yearlyCalendar = new TreeMap<>();
for (int month = 1; month <= 12; month++) {
YearMonth yearMonth = YearMonth.of(year, month);
LocalDate startDate = yearMonth.atDay(1);
LocalDate endDate = yearMonth.atEndOfMonth();
int weekNumber = 1;
List<Week> weeklyCalendar = new ArrayList<>();
List<LocalDate> dates = new ArrayList<>();
for (LocalDate date = startDate; date.isBefore(endDate.plusDays(1)); date = date.plusDays(1)) {
dates.add(date);
// 如果是周日,则表示一个完整的周结束,创建一个新的周
if (date.getDayOfWeek() == DayOfWeek.SUNDAY) {
weeklyCalendar.add(new Week(weekNumber, new ArrayList<>(dates)));
dates.clear();
weekNumber++;
}
}
// 处理最后一周不满一周的情况
if (!dates.isEmpty()) {
weeklyCalendar.add(new Week(weekNumber, dates));
}
yearlyCalendar.put(month, weeklyCalendar);
}
return yearlyCalendar;
}
static class Week {
private int weekNumber;
private List<LocalDate> dates;
public Week(int weekNumber, List<LocalDate> dates) {
this.weekNumber = weekNumber;
this.dates = dates;
}
public int getWeekNumber() {
return weekNumber;
}
public List<LocalDate> getDates() {
return dates;
}
}
// public static void main(String[] args) throws ParseException {
// String a="2022-6-1";
// String b="2017.3";
// String c="2021.11.1";
// String d="2021.1.1";
// String e="2020/11/1";
// String f="2020/7";
// String newtime=DateSplitUtil.strToDate(a);
// System.out.println("时间:"+newtime);
// newtime=DateSplitUtil.strToDate(b);
// System.out.println("时间:"+newtime);
// newtime=DateSplitUtil.strToDate(c);
// System.out.println("时间:"+newtime);
// newtime=DateSplitUtil.strToDate(d);
// System.out.println("时间:"+newtime);
// newtime=DateSplitUtil.strToDate(e);
// System.out.println("时间:"+newtime);
// newtime=DateSplitUtil.strToDate(f);
// System.out.println("时间:"+newtime);
//
// }
// public static void main(String[] args) {
// String s ="2023-01-11";
// String newtime=DateSplitUtil.addDayTime(s);
// System.out.println("字符串的长度为:"+s.length());
// System.out.println("新时间:"+newtime);
// }
// public static void main(String[] args) throws ParseException {
// String s =DateSplitUtil.getNowTime(1);
// System.out.println(s);
// }
// public static void main(String[] args) {
// /**
// * 判断当前时间是否在一个时间段内 HH:mm 格式
// */
// String strStartTime1 = "2024-1-1";
// String strEndTime1 = "2025-12-31";
//
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// String now = sdf.format(new Date());
// Date nowTime;
// try {
// nowTime = sdf.parse(now);
// Date startTime1 = sdf.parse(strStartTime1);
// Date endTime1 = sdf.parse(strEndTime1);
// // 注:也可以通过此方式判断当前时间是否具体时间段内 yyyy-MM-dd HH:mm:ss格式 [2022-03-09 12:00:00,2022-03-10 15:00:00]
// // 当前时间和时间段的格式保持一致即可判断
// if (isBeforeDate(nowTime, startTime1)) {
// System.out.println("当前时间在时间段内[" + isBeforeDate(nowTime, startTime1) + ",");
// } else {
// System.out.println("当前时间不再时间段内[" + isBeforeDate(nowTime, startTime1) + "]");
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// public static void main(String[] args) throws ParseException {
// String s=DateSplitUtil.downdate();
// String s1=DateSplitUtil.getChinaDateFromString(DateSplitUtil.downdate());
// System.out.println("结果输出"+DateSplitUtil.getChinaDateFromString(DateSplitUtil.downdate()));
// //2019-10-25 13:25:55
// DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
// Date start = dateFormat1.parse("2020-08-01");
// Date end = dateFormat1.parse("2025-08-05");
// LocalDateTime startTime = start.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
// LocalDateTime endTime = end.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
// //LocalDateTime startTime = LocalDateTime.of(2019, 10, 25, 13, 25, 55);
// //2020-01-25 12:30:32
// //LocalDateTime endTime = LocalDateTime.of(2020, 1, 25, 12, 30, 32);
// List<DateRange> dateMonthRanges = DateSplitUtil.splitDateRangeByYear1(startTime, endTime);
// System.out.println("按月切分后的结果:\n{}"+dateMonthRanges);
// System.out.println("按月切分后的结果:\n{}"+dateMonthRanges.size());
// }
}
下面是运行的效果截图: