1
/**
2
* 日期操作助手類
3
*
4
*
5
*/
6
public final class DateHelper {
7
8
/**
9
* 按照yyyy-MM-dd格式獲取當(dāng)前日期
10
*
11
* @return
12
*/
13
public static Date getDate(){
14
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
15
try {
16
return dateFormat.parse(dateFormat.format(new Date()));
17
} catch (ParseException e) {
18
e.printStackTrace();
19
return null;
20
}
21
}
22
23
/**
24
* 按照yyyy-MM-dd HH:mm:ss的格式獲取系統(tǒng)當(dāng)前時間
25
*
26
* @param format
27
* @param date
28
*/
29
public static Date getTime(){
30
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
31
try {
32
return dateFormat.parse(dateFormat.format(new Date()));
33
} catch (ParseException e) {
34
e.printStackTrace();
35
return null;
36
}
37
}
38
39
/**
40
* 按照指定格式將字符串轉(zhuǎn)換為日期
41
*
42
* @param format
43
* @param date
44
*/
45
public static Date getDate(String format, String source){
46
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
47
try {
48
if (StringUtils.isNotEmpty(source)){
49
return dateFormat.parse(source);
50
} else{
51
return null;
52
}
53
} catch (ParseException e) {
54
e.printStackTrace();
55
return null;
56
}
57
}
58
59
/**
60
* 將yyyy-MM-dd格式的字符串轉(zhuǎn)換為日期類型
61
*
62
* @param source
63
* @return
64
*/
65
public static Date getDate(String source){
66
if (source != null){
67
return getDate("yyyy-MM-dd", source);
68
} else{
69
return null;
70
}
71
}
72
73
/**
74
* 將yyyy-MM-dd HH:mm:ss格式的字符串轉(zhuǎn)換為日期類型
75
*
76
* @param source
77
* @return
78
*/
79
public static Date getTime(String source){
80
if (source != null){
81
return getDate("yyyy-MM-dd HH:mm:ss", source);
82
} else{
83
return null;
84
}
85
}
86
87
/**
88
* 將日期轉(zhuǎn)換為字符串類型
89
*
90
* @return
91
*/
92
public static String date2Char(Date date){
93
try{
94
if (date != null){
95
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
96
return format.format(date);
97
}
98
} catch (Exception e){
99
e.printStackTrace();
100
}
101
return null;
102
}
103
104
/**
105
* 將日期轉(zhuǎn)換為字符串類型的時間
106
*
107
* @return
108
*/
109
public static String time2Char(Date date){
110
try{
111
if (date != null){
112
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
113
return format.format(date);
114
}
115
} catch (Exception e){
116
e.printStackTrace();
117
}
118
return null;
119
}
120
121
/**
122
* 獲取兩個日期相差的天數(shù)
123
*
124
* @param date1 起始日期
125
* @param date2 截止日期
126
* @return
127
*/
128
public static Integer getDays(Date start, Date end){
129
try{
130
if (start == null || end == null){
131
return null;
132
}
133
SimpleDateFormat f1 = new SimpleDateFormat("yyyy-MM-dd");
134
SimpleDateFormat f2 = new SimpleDateFormat("yyyy-MM-dd");
135
Date d1 = f1.parse(f1.format(start));
136
Date d2 = f2.parse(f2.format(end));
137
int days = (int)((d2.getTime() - d1.getTime()) / (60000 * 60 * 24));
138
return days;
139
} catch (Exception e){
140
e.printStackTrace();
141
throw new RuntimeException("get days is error");
142
}
143
}
144
145
/**
146
* 按照指定的格式返回當(dāng)前日期的字符串表是形式
147
*
148
* @param format
149
* @return
150
*/
151
public static String getDateForChar(String format){
152
try{
153
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
154
return dateFormat.format(new Date());
155
} catch (Exception e){
156
e.printStackTrace();
157
return null;
158
}
159
}
160
161
/**
162
* 按照yyyy-MM-dd格式返回當(dāng)前日期的字符串表示形式
163
*
164
* @return
165
*/
166
public static String getDateForChar(){
167
return getDateForChar("yyyy-MM-dd");
168
}
169
170
/**
171
* 按照yyyy-MM-dd HH:mm:ss格式返回當(dāng)前時間的字符串表示形式
172
*
173
* @return
174
*/
175
public static String getTimeForChar(){
176
return getDateForChar("yyyy-MM-dd HH:mm:ss");
177
}
178
179
/**
180
* 為原日期添加指定的天數(shù)并返回添加后的日期,如果天數(shù)為負(fù)數(shù)則在原日期的基礎(chǔ)上減去指定的天數(shù)
181
*
182
* @param source
183
* @param days
184
* @return
185
*/
186
public static Date addDays(Date source, int days){
187
try{
188
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
189
format.parse(format.format(source));
190
Calendar calendar = format.getCalendar();
191
calendar.add(Calendar.DAY_OF_YEAR, days);
192
return calendar.getTime();
193
} catch (Exception e){
194
throw new RuntimeException("add days is error.");
195
}
196
}
197
198
/**
199
* 取得當(dāng)前日期的星期數(shù)
200
*
201
* @return
202
*/
203
public static String getWeek(){
204
Calendar calendar = Calendar.getInstance();
205
int week = calendar.get(Calendar.DAY_OF_WEEK);
206
switch (week) {
207
case Calendar.MONDAY:
208
return "星期一";
209
case Calendar.TUESDAY:
210
return "星期二";
211
case Calendar.WEDNESDAY:
212
return "星期三";
213
case Calendar.THURSDAY:
214
return "星期四";
215
case Calendar.FRIDAY:
216
return "星期五";
217
case Calendar.SATURDAY:
218
return "星期六";
219
case Calendar.SUNDAY:
220
return "星期日";
221
default:
222
return null;
223
}
224
}
225
226
/**
227
* 取得當(dāng)前日期的年
228
*
229
* @return
230
*/
231
public static String getYear(){
232
Calendar calendar = Calendar.getInstance();
233
int year = calendar.get(Calendar.YEAR);
234
return String.valueOf(year);
235
}
236
237
/**
238
* 取得當(dāng)前日期的月
239
*
240
* @return
241
*/
242
public static String getMonth(){
243
Calendar calendar = Calendar.getInstance();
244
int moth = calendar.get(Calendar.MONTH);
245
return String.valueOf(moth);
246
}
247
248
/**
249
* 取得當(dāng)前日期的月添加指定的天數(shù)并返回添加后的月,如果天數(shù)為負(fù)數(shù)則在原日期的基礎(chǔ)上減去指定的天數(shù)
250
* @param days
251
* @return
252
*/
253
public static String getAddDaysMonth(int days){
254
Calendar calendar = Calendar.getInstance();
255
calendar.add(Calendar.DAY_OF_YEAR, days);
256
int moth = calendar.get(Calendar.MONTH);
257
return String.valueOf(moth);
258
}
259
260
/**
261
* 取得當(dāng)前日期的月添加指定的天數(shù)并返回添加后的年,如果天數(shù)為負(fù)數(shù)則在原日期的基礎(chǔ)上減去指定的天數(shù)
262
* @param days
263
* @return
264
*/
265
public static String getAddDaysYear(int days){
266
Calendar calendar = Calendar.getInstance();
267
calendar.add(Calendar.DAY_OF_YEAR, days);
268
int moth = calendar.get(Calendar.YEAR);
269
return String.valueOf(moth);
270
}
271
272
public static void main(String[] args){
273
System.out.println(getYear());
274
System.out.println(getMonth());
275
System.out.println(getAddDaysMonth(-30));
276
}
277
278
}
279

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

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279
