DateFormater.java
13 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package com.sitech.util;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Calendar;
public class DateFormater {
public final static String INVALIDDATE = "1950-01-01";
public final static String SQLTYPE = "ORACLE";
public final static String getFormatTime(String formatType) {
SimpleDateFormat formatter = new SimpleDateFormat(formatType);
return formatter.format(new java.util.Date());
}
public final static String getFormatTime(String formatType, java.util.Date formatDate) {
if (null == formatDate) {
return "";
}
SimpleDateFormat formatter = new SimpleDateFormat(formatType);
return formatter.format(formatDate);
}
//判断日期字符串(yyyymmdd)是否正确(不是很严禁!没有对日期的30、31、28、29进行判断)
public final static boolean isRightDate(String date_s) {
boolean isRight = true;
if (date_s.length() != 8) {
isRight = false;
} else {
String year_s = date_s.substring(0, 4);
String month_s = date_s.substring(4, 6);
String date_ss = date_s.substring(6, 8);
try {
int year_i = Integer.parseInt(year_s);
int month_i = Integer.parseInt(month_s);
int date_i = Integer.parseInt(date_ss);
if (year_i < 1000) {
isRight = false;
}
if (month_i < 1 || month_i > 12) {
isRight = false;
}
if (date_i < 1 || date_i > 31) {
isRight = false;
}
} catch (Exception e) {
isRight = false;
e.printStackTrace();
}
}
return isRight;
}
public final static String getNowTime() {
SimpleDateFormat formatter1
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter1.format(new Date());
}
public final static String getToday() {
SimpleDateFormat formatter1
= new SimpleDateFormat("yyyy-MM-dd");
return formatter1.format(new Date());
}
/*
如果时间为默认值INVALIDDATE,则在前台显示""
*/
public final static String dateToString(Date dDate) throws RuntimeException {
if (null == dDate) {
return "";
}
SimpleDateFormat formatter1
= new SimpleDateFormat("yyyy-MM-dd");
if (formatter1.format(dDate).equals(INVALIDDATE)) {
return "";
}
return formatter1.format(dDate);
}
/*
如果时间为默认值INVALIDDATE,则在前台显示""
*/
public final static String datetimeToString(Date dDate) throws RuntimeException {
if (null == dDate) {
return "";
}
if (dateToString(dDate).equals("")) {
return "";
}
SimpleDateFormat formatter1
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter1.format(dDate);
}
public final static Date stringToDate(String cDate, String cFormat) throws ParseException {
SimpleDateFormat formatter1 = new SimpleDateFormat(cFormat);
return formatter1.parse(cDate);
}
public final static Date stringToDate(String cDate) throws ParseException {
try {
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
return formatter1.parse(cDate);
}
catch (ParseException e) {
try {
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter1.parse(cDate);
}
catch (ParseException ee) {
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return formatter1.parse(cDate);
}
}
}
public final static Date stringToDateTime(String cDate) throws ParseException {
try {
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter1.parse(cDate);
}
catch (ParseException e) {
try {
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return formatter1.parse(cDate);
}
catch (ParseException ee) {
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
return formatter1.parse(cDate);
}
}
}
private final static String toOracleDate(Date dDate) throws ParseException {
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
return " to_date('" + formatter1.format(dDate) + "','yyyy-mm-dd') ";
}
public final static String toOracleDateTime(Date dDate) throws ParseException {
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return " to_date('" + formatter1.format(dDate) + "','yyyy-MM-dd HH24:mi:ss') ";
}
private final static String toSybaseDate(Date dDate) throws ParseException {
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
return " '" + formatter1.format(dDate) + "' ";
}
private final static String toSybaseDateTime(Date dDate) throws ParseException {
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
return " '" + formatter1.format(dDate) + "' ";
}
public final static String toDBDate(Date dDate) throws ParseException {
if (null == dDate) {
dDate = stringToDate(INVALIDDATE);
}
String cSQLDate = null;
if (SQLTYPE.equals("ORACLE"))
cSQLDate = toOracleDate(dDate);
if (SQLTYPE.equals("SYBASE"))
cSQLDate = toSybaseDate(dDate);
return cSQLDate;
}
public final static String toDBDate(String cDate) throws ParseException {
if (null == cDate) {
cDate = INVALIDDATE;
}
return toDBDate(stringToDate(cDate));
}
public final static String toDBDateTime(Date dDate) throws ParseException {
if (null == dDate) {
dDate = stringToDate(INVALIDDATE);
}
String cSQLDate = null;
if (SQLTYPE.equals("ORACLE"))
cSQLDate = toOracleDateTime(dDate);
if (SQLTYPE.equals("SYBASE"))
cSQLDate = toSybaseDateTime(dDate);
return cSQLDate;
}
public final static String toDBDateTime(String cDate) throws ParseException {
if (null == cDate) {
cDate = INVALIDDATE;
}
return toDBDateTime(stringToDateTime(cDate));
}
/**
* (1) parameter description
*
* @param date
* @param type
* @param timeInterval
* @return (2)function description
* realize the time plus and minus
* (3) loop condition
* y: year
* m: month
* d: date
* h:hour
* f: minute(fenzhong)
* s: second
*/
public static Date computeDate(Date date, char type, int timeInterval) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int Time_year = cal.get(Calendar.YEAR);
int Time_month = cal.get(Calendar.MONTH);
int Time_day = cal.get(Calendar.DAY_OF_MONTH);
int Time_hour = cal.get(Calendar.HOUR_OF_DAY);
int Time_minute = cal.get(Calendar.MINUTE);
int Time_second = cal.get(Calendar.SECOND);
switch (type) {
case 'y': {
Time_year = Time_year + timeInterval;
cal.set(Calendar.YEAR, Time_year);
}
break;
case 'm': {
Time_month = Time_month + timeInterval;
cal.set(Calendar.MONTH, Time_month);
}
break;
case 'd': {
Time_day = Time_day + timeInterval;
cal.set(Calendar.DAY_OF_MONTH, Time_day);
}
break;
case 'h': {
Time_hour = Time_hour + timeInterval;
cal.set(Calendar.HOUR_OF_DAY, Time_hour);
}
break;
case 'f': {
Time_minute = Time_minute + timeInterval;
cal.set(Calendar.MINUTE, Time_minute);
}
break;
case 's': {
Time_second = Time_second + timeInterval;
cal.set(Calendar.SECOND, Time_second);
}
break;
default:
break;
}
date = cal.getTime();
return date;
}
public static Date computeDate(String cDate, char type, int timeInterval) {
java.util.Date dDate = null;
try {
dDate = stringToDateTime(cDate);
}
catch (ParseException e) {
dDate = new Date();
}
return computeDate(dDate, type, timeInterval);
}
public final static String getHISTableName(Date dDate) throws ParseException {
if (null == dDate) {
dDate = new Date();
}
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMMdd");
return formatter1.format(dDate);
}
/**
* 根据传入的时间和需计算的差值,获取计算后的时间
* 算法:通过转化成标准毫秒进行转换。
* add by qiuzq 20080611.
*
* @param dDate 传入的时间
* @param calValue 需计算的差值
* @return 返回字符串
* @throws ParseException
*/
public final static String getCalDateStr(Date dDate, int calValue) throws ParseException {
SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long myTime = dDate.getTime() + calValue;
dDate.setTime(myTime);
return bartDateFormat.format(dDate);
}
/**
* 根据传入的时间、类型和需计算的差值,获取计算后的时间
* 算法:通过转化成标准毫秒进行转换。
*
* @param sDate
* @param type 类型,最大粒度到天;目前不支持年月
* @param calValue
* @return
* @throws ParseException
*/
public final static String getCalDateStr(String sDate, String type, int calValue) throws ParseException {
SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
int iValue = 0;
if ("day".equals(type.toLowerCase())) {
iValue = 24 * 60 * 60 * 1000 * calValue;
} else if ("hour".equals(type.toLowerCase())) {
iValue = 60 * 60 * 1000 * calValue;
} else if ("minute".equals(type.toLowerCase())) {
iValue = 60 * 1000 * calValue;
} else if ("second".equals(type.toLowerCase())) {
iValue = 1000 * calValue;
}
return getCalDateStr(bartDateFormat.parse(sDate), iValue);
}
/**
* 根据传入的时间、类型和需计算的差值,获取计算后的时间
* 算法:通过转化成标准毫秒进行转换。
*
* @param dDate
* @param type 类型,最大粒度到天;目前不支持年月
* @param calValue
* @return
* @throws ParseException
*/
public final static String getCalDateStr(Date dDate, String type, int calValue) throws ParseException {
int iValue = 0;
if ("day".equals(type.toLowerCase())) {
iValue = 24 * 60 * 60 * 1000 * calValue;
} else if ("hour".equals(type.toLowerCase())) {
iValue = 60 * 60 * 1000 * calValue;
} else if ("minute".equals(type.toLowerCase())) {
iValue = 60 * 1000 * calValue;
} else if ("second".equals(type.toLowerCase())) {
iValue = 1000 * calValue;
}
return getCalDateStr(dDate, iValue);
}
/**
* 根据传入的时间、类型和需计算的差值,获取计算后的时间
* 算法:通过转化成标准毫秒进行转换。
*
* @param dDate
* @param type 类型,最大粒度到天;目前不支持年月
* @param calValue
* @return
* @throws ParseException
*/
public final static Date getCalDateTime(Date dDate, String type, int calValue) throws ParseException {
int iValue = 0;
if ("day".equals(type.toLowerCase())) {
iValue = 24 * 60 * 60 * 1000 * calValue;
} else if ("hour".equals(type.toLowerCase())) {
iValue = 60 * 60 * 1000 * calValue;
} else if ("minute".equals(type.toLowerCase())) {
iValue = 60 * 1000 * calValue;
} else if ("second".equals(type.toLowerCase())) {
iValue = 1000 * calValue;
}
long myTime = dDate.getTime() + iValue;
dDate.setTime(myTime);
return dDate;
}
}