ConstDateUtil.java
2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.ruoyi.utils;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class ConstDateUtil {
public static String formatDate(String pattern){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
return simpleDateFormat.format(new Date());
}
public static String formatDate(String pattern,Date date){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
return simpleDateFormat.format(date);
}
public static String formatDate(long time){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
return simpleDateFormat.format(new Date(time));
}
public static String formatDate(Date date){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
return simpleDateFormat.format(date);
}
public static Date dateAddition(String dateString,String timeString){
LocalDate date = LocalDate.parse(dateString);
LocalTime time = LocalTime.parse(timeString);
// 将日期和时间相加
LocalDateTime dateTime = date.atTime(time);
return Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
}
public static Date getTheSpecifiedNumberOfDaysOfTime(Integer amount) {
// 获取当前日期时间
Calendar calendar = Calendar.getInstance();
// 将日期减去一天
calendar.add(Calendar.DAY_OF_MONTH, amount);
// 获取昨天的日期时间
return calendar.getTime();
}
public static LocalDateTime getLocalDateTimeByLongTime(long time){
Instant instant = Instant.ofEpochMilli(time);
LocalDateTime localDateTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
return localDateTime;
}
public static List<LocalDate> getDateSetFromTheCurrentDayToTheEndOfTheMonth(){
LocalDate today = LocalDate.now();
int year = today.getYear();
int month = today.getMonthValue();
// 获取本月的最后一天
LocalDate endDay;
if (month == 12) {
endDay = LocalDate.of(year, month, 31);
} else {
endDay = LocalDate.of(year, month + 1, 1).minusDays(1);
}
// 生成日期列表
List<LocalDate> dateList = new ArrayList<>();
LocalDate currentDay = today;
while (!currentDay.isAfter(endDay)) {
dateList.add(currentDay);
currentDay = currentDay.plusDays(1);
}
return dateList;
}
}