-
Notifications
You must be signed in to change notification settings - Fork 0
/
DateMathUtils.java
152 lines (135 loc) · 3.84 KB
/
DateMathUtils.java
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.suixingpay.datas.manager.core.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @author fuzizheng[[email protected]]
*/
public class DateMathUtils {
public final static String PATTERN_LONG_DAY = "yyyy-MM-dd";
public final static String PATTERN_SHORT_DAY = "yyyyMMdd";
public final static String PATTERN_LONG_MONTH = "yyyy-MM";
public final static String PATTERN_SHORT_MONTH = "yyyyMM";
public final static String PATTERN_LONG_SECOND = "yyyy-MM-dd HH:mm:ss";
public final static String PATTERN_LONG_MILLISECOND = "yyyy-MM-dd HH:mm:ss:SSS";
/**
* 日期格式转化
*
* @param pattern
* @param date
* @return
*/
public static Date parseDate(String pattern, String date) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date d = null;
try {
d = sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return d;
}
/**
* 日期运算
*
* @param date
* @param calendarType
* @param account
* @return
*/
public static Date mathDate(Date date, int calendarType, int account) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(calendarType, account);
return calendar.getTime();
}
/**
* 获取加减年份后的日期 String
*
* @param pattern
* @param date
* @param year
* @return
*/
public static String dateAddYears(String pattern, String date, int year) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(mathDate(parseDate(pattern, date), Calendar.YEAR, year));
}
/**
* 获取加减年份后的日期 Date
*
* @param date
* @param year
* @return
*/
public static Date dateAddYears(Date date, int year) {
return mathDate(date, Calendar.YEAR, year);
}
/**
* 获取加减月份后的日期 String
*
* @param pattern
* @param date
* @param month
* @return
*/
public static String dateAddMonths(String pattern, String date, int month) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(mathDate(parseDate(pattern, date), Calendar.MONTH, month));
}
/**
* 获取加减月份后的日期 Date
*
* @param date
* @param month
* @return
*/
public static Date dateAddMonths(Date date, int month) {
return mathDate(date, Calendar.MONTH, month);
}
/**
* 获取加减天数后的日期 String
*
* @param pattern
* @param date
* @param day
* @return
*/
public static String dateAddDays(String pattern, String date, int day) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(mathDate(parseDate(pattern, date), Calendar.DAY_OF_YEAR, day));
}
/**
* 获取加减天数后的日期 Date
*
* @param date
* @param day
* @return
*/
public static Date dateAddDays(Date date, int day) {
return mathDate(date, Calendar.DAY_OF_YEAR, day);
}
/**
* 获取加减分钟后的日期 String
*
* @param pattern
* @param date
* @param minute
* @return
*/
public static String dateAddMinutes(String pattern, String date, int minute) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(mathDate(parseDate(pattern, date), Calendar.MINUTE, minute));
}
/**
* 获取加减分钟后的日期 Date
*
* @param date
* @param minute
* @return
*/
public static Date dateAddMinutes(Date date, int minute) {
return mathDate(date, Calendar.MINUTE, minute);
}
}