package com.cloudroam.util;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import java.io.File;
|
import java.nio.file.spi.FileTypeDetector;
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.time.*;
|
import java.time.format.DateTimeFormatter;
|
import java.time.temporal.TemporalAdjusters;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
|
@Slf4j
|
public class DateUtils {
|
public static void main(String[] args) {
|
// List<String > days=getCurrentMonthsDays();
|
// List<String > days=getCurrentWeekDays();
|
// List<String> days=getNextWeekDays();
|
/*List<String> days=getDateRangeDays("2023-08-01","2023-08-10");
|
|
for(String day:days){
|
System.out.println(day);
|
}
|
*/
|
|
// System.out.println(formattedTodayDate());
|
|
// System.out.println(formattedYesterdayDate());
|
|
|
// days=getPreViousWeekDays();
|
|
// days=getPreviousMonthsDays();
|
//
|
//
|
//
|
// for(String day:days){
|
// System.out.println(day);
|
// }
|
|
|
// System.out.println(stringToLocalDateTime("2023-08-17"));
|
|
System.out.println(getCurrentDateCatalog());
|
|
|
}
|
|
|
/**
|
* 获取今天日期
|
* @return
|
*/
|
public static String formattedTodayDate(){
|
LocalDate currentDate = LocalDate.now();
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
String formattedDate = currentDate.format(formatter);
|
return formattedDate;
|
}
|
|
/**
|
* 获取昨天日期
|
* @return
|
*/
|
public static String formattedYesterdayDate(){
|
LocalDate yesterday = LocalDate.now().minusDays(1); // 获取昨天日期
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
String formattedYesterday = yesterday.format(formatter);
|
return formattedYesterday;
|
}
|
|
|
|
/**
|
* 获取本月日期列表
|
* @return
|
*/
|
public static List<String> getPreviousMonthsDays(){
|
LocalDate currentDate = LocalDate.now();
|
|
// 获取上个月的年月
|
YearMonth lastMonth = YearMonth.from(currentDate.minusMonths(1));
|
|
// 获取上个月的天数
|
int lastMonthDays = lastMonth.lengthOfMonth();
|
|
// 存储日期的列表
|
List<LocalDate> lastMonthDates = new ArrayList<>();
|
|
// 循环遍历从上个月的第一天到最后一天,将日期添加到列表中
|
LocalDate date = LocalDate.of(lastMonth.getYear(), lastMonth.getMonth(), 1);
|
for (int i = 0; i < lastMonthDays; i++) {
|
lastMonthDates.add(date);
|
date = date.plusDays(1);
|
}
|
|
List<String> dateStrList=new ArrayList<>();
|
// 打印日期列表
|
for (LocalDate d : lastMonthDates) {
|
dateStrList.add(d.toString());
|
}
|
return dateStrList;
|
}
|
|
/**
|
* 获取本月日期列表
|
* @return
|
*/
|
public static List<String> getCurrentMonthsDays(){
|
// 获取当前年月
|
YearMonth currentYearMonth = YearMonth.now();
|
int year = currentYearMonth.getYear();
|
int month = currentYearMonth.getMonthValue();
|
|
// 获取本月的第一天和最后一天
|
LocalDate firstDay = LocalDate.of(year, month, 1);
|
LocalDate lastDay = currentYearMonth.atEndOfMonth();
|
|
// 存储日期的数组
|
List<LocalDate> allDates = new ArrayList<>();
|
|
// 循环遍历从第一天到最后一天,将日期添加到数组中
|
LocalDate currentDate = firstDay;
|
while (!currentDate.isAfter(lastDay)) {
|
allDates.add(currentDate);
|
currentDate = currentDate.plusDays(1);
|
}
|
|
// 将日期数组转换为数组
|
LocalDate[] datesArray = allDates.toArray(new LocalDate[0]);
|
List<String> dateStrList=new ArrayList<>();
|
for (LocalDate date : datesArray) {
|
dateStrList.add(date.toString());
|
}
|
|
return dateStrList;
|
}
|
|
/**
|
* 获取本周日期列表
|
* @return
|
*/
|
public static List<String> getCurrentWeekDays(){
|
// 获取当前日期
|
LocalDate currentDate = LocalDate.now();
|
|
// 计算本周的第一天和最后一天
|
LocalDate firstDayOfWeek = currentDate.with(DayOfWeek.MONDAY);
|
LocalDate lastDayOfWeek = currentDate.with(DayOfWeek.SUNDAY);
|
|
// 存储日期的数组
|
List<LocalDate> allDates = new ArrayList<>();
|
|
// 循环遍历从本周第一天到最后一天,将日期添加到数组中
|
LocalDate date = firstDayOfWeek;
|
while (!date.isAfter(lastDayOfWeek)) {
|
allDates.add(date);
|
date = date.plusDays(1);
|
}
|
|
// 将日期数组转换为数组
|
LocalDate[] datesArray = allDates.toArray(new LocalDate[0]);
|
List<String> dateStrList=new ArrayList<>();
|
// 打印日期数组
|
for (LocalDate d : datesArray) {
|
dateStrList.add(d.toString());
|
}
|
|
return dateStrList;
|
}
|
|
/**
|
* 获取上周日期列表
|
* @return
|
*/
|
public static List<String> getPreViousWeekDays(){
|
// 获取当前日期
|
LocalDate currentDate = LocalDate.now();
|
|
// 计算下周的第一天和最后一天
|
LocalDate firstDayOfNextWeek = currentDate.minusWeeks(1).with(TemporalAdjusters.previous(DayOfWeek.MONDAY));
|
LocalDate lastDayOfNextWeek = firstDayOfNextWeek.with(DayOfWeek.SUNDAY);
|
|
// 存储日期的数组
|
List<LocalDate> allDates = new ArrayList<>();
|
|
// 循环遍历从下周第一天到最后一天,将日期添加到数组中
|
LocalDate date = firstDayOfNextWeek;
|
while (!date.isAfter(lastDayOfNextWeek)) {
|
allDates.add(date);
|
date = date.plusDays(1);
|
}
|
|
// 将日期数组转换为数组
|
LocalDate[] datesArray = allDates.toArray(new LocalDate[0]);
|
List<String> dateStrList=new ArrayList<>();
|
// 打印日期数组
|
for (LocalDate d : datesArray) {
|
dateStrList.add(d.toString());
|
}
|
|
return dateStrList;
|
}
|
/**
|
* 获取下周日期列表
|
* @return
|
*/
|
public static List<String> getNextWeekDays(){
|
// 获取当前日期
|
LocalDate currentDate = LocalDate.now();
|
|
// 计算下周的第一天和最后一天
|
LocalDate firstDayOfNextWeek = currentDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
|
LocalDate lastDayOfNextWeek = firstDayOfNextWeek.with(DayOfWeek.SUNDAY);
|
|
// 存储日期的数组
|
List<LocalDate> allDates = new ArrayList<>();
|
|
// 循环遍历从下周第一天到最后一天,将日期添加到数组中
|
LocalDate date = firstDayOfNextWeek;
|
while (!date.isAfter(lastDayOfNextWeek)) {
|
allDates.add(date);
|
date = date.plusDays(1);
|
}
|
|
// 将日期数组转换为数组
|
LocalDate[] datesArray = allDates.toArray(new LocalDate[0]);
|
List<String> dateStrList=new ArrayList<>();
|
// 打印日期数组
|
for (LocalDate d : datesArray) {
|
dateStrList.add(d.toString());
|
}
|
|
return dateStrList;
|
}
|
|
public static List<String> getDateRangeDays(String startDateStr,String endDateStr ){
|
// 解析开始时间和结束时间字符串为LocalDate对象
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
LocalDate startDate = LocalDate.parse(startDateStr, formatter);
|
LocalDate endDate = LocalDate.parse(endDateStr, formatter);
|
|
// 存储日期的数组
|
List<LocalDate> dateArray = new ArrayList<>();
|
|
// 循环遍历从开始时间到结束时间,将日期添加到数组中
|
LocalDate currentDate = startDate;
|
while (!currentDate.isAfter(endDate)) {
|
dateArray.add(currentDate);
|
currentDate = currentDate.plusDays(1);
|
}
|
|
List<String> dateStrList=new ArrayList<>();
|
// 打印日期数组
|
for (LocalDate date : dateArray) {
|
dateStrList.add(date.toString());
|
}
|
|
return dateStrList;
|
}
|
|
|
/**
|
* 根据给定的小时数获取当天的指定小时数的时间
|
* @param givenHours
|
* @return
|
*/
|
public static LocalDateTime getTodayPointHourTime(int givenHours){
|
LocalDate currentDate = LocalDate.now();
|
// 构造当天的特定时间
|
LocalTime specificTime = LocalTime.of(givenHours, 0);
|
return LocalDateTime.of(currentDate, specificTime);
|
}
|
|
public static LocalDateTime stringToLocalDateTime(String dateString){
|
DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
LocalDate localDate = LocalDate.parse(dateString,formatter);
|
// 构造当天的特定时间
|
LocalDateTime localDateTime=localDate.atStartOfDay();
|
return localDateTime;
|
}
|
|
public static String dateToStr(Date date){
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
String strDate = sdf.format(date);
|
return strDate;
|
}
|
|
|
public static boolean isWeekend(LocalDate date) {
|
DayOfWeek dayOfWeek = date.getDayOfWeek();
|
return dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY;
|
}
|
|
public static String getTodayString(){
|
LocalDate today = LocalDate.now();
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
String formattedDate = today.format(formatter);
|
|
return formattedDate;
|
}
|
|
|
/**
|
* 将Date日期的yyyy-MM-dd HH:mm:ss 的格式转成 yyyy-MM-dd的date
|
* @param dailyDate yyyy-MM-dd HH:mm:ss 的日期
|
* @return yyyy-MM-dd的date
|
*/
|
public static Date formatDateToDateYYYYMMDD(Date dailyDate){
|
// 创建一个 SimpleDateFormat 对象,用于定义日期时间的输出格式
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
// 使用 SimpleDateFormat 格式化 Date 对象为只包含年月日的字符串
|
String formattedDate = sdf.format(dailyDate);
|
|
|
// 将格式化后的字符串重新解析为 Date 对象,只包含年月日部分
|
|
Date parsedDate = null;
|
try {
|
parsedDate = sdf.parse(formattedDate);
|
return parsedDate;
|
} catch (ParseException e) {
|
log.error(e.getMessage());
|
return null;
|
}
|
|
}
|
|
|
/**
|
* 判断一个日期是否在开始时间和结束时间内
|
* @param startTime 开始时间
|
* @param endTime 结束时间
|
* @param targetDate
|
* @return
|
*/
|
public static boolean isDateInRange(String startTime, String endTime, String targetDate) {
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
LocalDate startDate = LocalDate.parse(startTime, formatter);
|
LocalDate endDate = LocalDate.parse(endTime, formatter);
|
LocalDate target = LocalDate.parse(targetDate, formatter);
|
|
return target.isAfter(startDate) && target.isBefore(endDate);
|
}
|
|
/**
|
* 判断一个日期是否在开始时间和结束时间内
|
* @param startTime 开始时间
|
* @param endTime 结束时间
|
* @param targetDate
|
* @return
|
*/
|
public static boolean isDateInRange(String startTime, String endTime, Date targetDate) {
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
String targetDateStr = sdf.format(targetDate);
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
LocalDate startDate = LocalDate.parse(startTime, formatter);
|
LocalDate endDate = LocalDate.parse(endTime, formatter);
|
LocalDate target = LocalDate.parse(targetDateStr, formatter);
|
|
return target.isAfter(startDate) && target.isBefore(endDate);
|
}
|
|
|
public static boolean dateCompare( Date targetDate,String startTime) {
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
String targetDateStr = sdf.format(targetDate);
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
LocalDate startDate = LocalDate.parse(startTime, formatter);
|
|
LocalDate target = LocalDate.parse(targetDateStr, formatter);
|
// System.out.println(target.compareTo(startDate));
|
// return target.isAfter(startDate);
|
return target.compareTo(startDate)>=0;
|
}
|
|
/**
|
* 获取当前年月日,yyyy/MM/dd格式
|
* @return yyyy/MM/dd的字符串
|
*/
|
public static String getCurrentDateCatalog() {
|
|
Date date = new Date();
|
|
// 使用SimpleDateFormat类来格式化日期
|
SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
|
|
return sdf.format(date);
|
}
|
|
|
}
|