ConstDateUtil.java
3.91 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.ruoyi.utils;
import java.text.ParseException;
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 Date parseDate(String date){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return simpleDateFormat.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
public static Date parseDate(String date,String pattern){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
try {
return simpleDateFormat.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
public static String formatDate(String pattern,Date date){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
return simpleDateFormat.format(date);
}
public static String formatDate(String pattern,LocalDate 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;
}
/**
* 获取指定月份所有日期
*/
public static List<LocalDate> getAllDatesOfTheMonth(int year,int month){
YearMonth yearMonth = YearMonth.of(year, month);
int daysInMonth = yearMonth.lengthOfMonth();
List<LocalDate> dateList = new ArrayList<>(30);
for (int day = 1; day <= daysInMonth; day++) {
LocalDate date = LocalDate.of(year, month, day);
dateList.add(date);
}
return dateList;
}
}