您的位置:首页 > 编程语言 > Java开发

java 日期操作 DateUtil

2010-11-25 21:51 579 查看
java 的日期没有C#好操作,哎
Java Code

1
2
3 import java.text.SimpleDateFormat;
4 import java.text.ParseException;
5 import java.util.Calendar;
6 import java.util.Date;
7
8 public class DateUtils {
9
10 public final static String YYYY = "yyyy";
11 public final static String MM = "MM";
12 public final static String DD = "dd";
13 public final static String YYYY_MM_DD = "yyyy-MM-dd";
14 public final static String YYYY_MM = "yyyy-MM";
15 public final static String HH_MM_SS = "HH:mm:ss";
16 public final static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
17
18 public static String formatStr_yyyyMMddHHmmssS = "yyyy-MM-dd HH:mm:ss.S";
19 public static String formatStr_yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
20 public static String formatStr_yyyyMMddHHmm = "yyyy-MM-dd HH:mm";
21 public static String formatStr_yyyyMMddHH = "yyyy-MM-dd HH";
22 public static String formatStr_yyyyMMdd = "yyyy-MM-dd";
23 public static String[] formatStr = { formatStr_yyyyMMddHHmmss,
24 formatStr_yyyyMMddHHmm, formatStr_yyyyMMddHH, formatStr_yyyyMMdd };
25
26 /**
27 * 构造函数
28 */
29 public DateUtils() {
30 }
31
32 /**
33 * 日期格式化-将<code>Date</code>类型的日期格式化为<code>String</code>型
34 *
35 * @param date
36 * 待格式化的日期
37 *@param pattern
38 * 时间样式
39 *@return 一个被格式化了的<code>String</code>日期
40 */
41 public static String format(Date date, String pattern) {
42 if (date == null)
43 return "";
44 else
45 return getFormatter(pattern).format(date);
46 }
47
48 /**
49 * 默认把日期格式化成yyyy-mm-dd格式
50 *
51 * @param date
52 * @return
53 */
54 public static String format(Date date) {
55 if (date == null)
56 return "";
57 else
58 return getFormatter(YYYY_MM_DD).format(date);
59 }
60
61 /**
62 * 把字符串日期默认转换为yyyy-mm-dd格式的Data对象
63 *
64 * @param strDate
65 * @return
66 */
67 public static Date format(String strDate) {
68 Date d = null;
69 if (strDate == "")
70 return null;
71 else
72 try {
73 d = getFormatter(YYYY_MM_DD).parse(strDate);
74 } catch (ParseException pex) {
75 return null;
76 }
77 return d;
78 }
79
80 /**
81 * 把字符串日期转换为f指定格式的Data对象
82 *
83 * @param strDate
84 * ,f
85 * @return
86 */
87 public static Date format(String strDate, String f) {
88 Date d = null;
89 if (strDate == "")
90 return null;
91 else
92 try {
93 d = getFormatter(f).parse(strDate);
94 } catch (ParseException pex) {
95 return null;
96 }
97 return d;
98 }
99
100 /**
101 * 日期解析-将<code>String</code>类型的日期解析为<code>Date</code>型
102 *
103 * @param date
104 * 待格式化的日期
105 *@param pattern
106 * 日期样式
107 *@exception ParseException
108 * 如果所给的字符串不能被解析成一个日期
109 *@return 一个被格式化了的<code>Date</code>日期
110 */
111 public static Date parse(String strDate, String pattern)
112 throws ParseException {
113 try {
114 return getFormatter(pattern).parse(strDate);
115 } catch (ParseException pe) {
116 throw new ParseException(
117 "Method parse in Class DateUtils err: parse strDate fail.",
118 pe.getErrorOffset());
119 }
120 }
121
122 /**
123 * 获取当前日期
124 *
125 * @return 一个包含年月日的<code>Date</code>型日期
126 */
127 public static synchronized Date getCurrDate() {
128 Calendar calendar = Calendar.getInstance();
129 return calendar.getTime();
130 }
131
132 /**
133 * 获取当前日期
134 *
135 * @return 一个包含年月日的<code>String</code>型日期,但不包含时分秒。yyyy-mm-dd
136 */
137 public static String getCurrDateStr() {
138 return format(getCurrDate(), YYYY_MM_DD);
139 }
140
141 /**
142 * 获取当前时间
143 *
144 * @return 一个包含年月日时分秒的<code>String</code>型日期。hh:mm:ss
145 */
146 public static String getCurrTimeStr() {
147 return format(getCurrDate(), HH_MM_SS);
148 }
149
150 /**
151 * 获取当前完整时间,样式: yyyy-MM-dd hh:mm:ss
152 *
153 * @return 一个包含年月日时分秒的<code>String</code>型日期。yyyy-MM-dd hh:mm:ss
154 */
155 public static String getCurrDateTimeStr() {
156 return format(getCurrDate(), YYYY_MM_DD_HH_MM_SS);
157 }
158
159 /**
160 * 获取当前年分 样式:yyyy
161 *
162 * @return 当前年分
163 */
164 public static String getYear() {
165 return format(getCurrDate(), YYYY);
166 }
167
168 /**
169 * 获取当前月分 样式:MM
170 *
171 * @return 当前月分
172 */
173 public static String getMonth() {
174 return format(getCurrDate(), MM);
175 }
176
177 /**
178 * 获取当前日期号 样式:dd
179 *
180 * @return 当前日期号
181 */
182 public static String getDay() {
183 return format(getCurrDate(), DD);
184 }
185
186 /**
187 * 按给定日期样式判断给定字符串是否为合法日期数据
188 *
189 * @param strDate
190 * 要判断的日期
191 *@param pattern
192 * 日期样式
193 *@return true 如果是,否则返回false
194 */
195 public static boolean isDate(String strDate, String pattern) {
196 try {
197 parse(strDate, pattern);
198 return true;
199 } catch (ParseException pe) {
200 return false;
201 }
202 }
203
204 /**
205 * 判断给定字符串是否为特定格式日期(包括:年月日yyyy-MM-dd)数据
206 *
207 * @param strDate
208 * 要判断的日期
209 *@return true 如果是,否则返回false
210 */
211 // public static boolean isDate(String strDate) {
212 // try {
213 // parse(strDate, YYYY_MM_DD);
214 // return true;
215 // }
216 // catch (ParseException pe) {
217 // return false;
218 // }
219 // }
220
221 /**
222 * 判断给定字符串是否为特定格式年份(格式:yyyy)数据
223 *
224 * @param strDate
225 * 要判断的日期
226 *@return true 如果是,否则返回false
227 */
228 public static boolean isYYYY(String strDate) {
229 try {
230 parse(strDate, YYYY);
231 return true;
232 } catch (ParseException pe) {
233 return false;
234 }
235 }
236
237 public static boolean isYYYY_MM(String strDate) {
238 try {
239 parse(strDate, YYYY_MM);
240 return true;
241 } catch (ParseException pe) {
242 return false;
243 }
244 }
245
246 /**
247 * 判断给定字符串是否为特定格式的年月日(格式:yyyy-MM-dd)数据
248 *
249 * @param strDate
250 * 要判断的日期
251 *@return true 如果是,否则返回false
252 */
253 public static boolean isYYYY_MM_DD(String strDate) {
254 try {
255 parse(strDate, YYYY_MM_DD);
256 return true;
257 } catch (ParseException pe) {
258 return false;
259 }
260 }
261
262 /**
263 * 判断给定字符串是否为特定格式年月日时分秒(格式:yyyy-MM-dd HH:mm:ss)数据
264 *
265 * @param strDate
266 * 要判断的日期
267 *@return true 如果是,否则返回false
268 */
269 public static boolean isYYYY_MM_DD_HH_MM_SS(String strDate) {
270 try {
271 parse(strDate, YYYY_MM_DD_HH_MM_SS);
272 return true;
273 } catch (ParseException pe) {
274 return false;
275 }
276 }
277
278 /**
279 * 判断给定字符串是否为特定格式时分秒(格式:HH:mm:ss)数据
280 *
281 * @param strDate
282 * 要判断的日期
283 *@return true 如果是,否则返回false
284 */
285 public static boolean isHH_MM_SS(String strDate) {
286 try {
287 parse(strDate, HH_MM_SS);
288 return true;
289 } catch (ParseException pe) {
290 return false;
291 }
292 }
293
294 /**
295 * 判断给定字符串是否为特定格式时间(包括:时分秒hh:mm:ss)数据
296 *
297 * @param strTime
298 * 要判断的时间
299 *@return true 如果是,否则返回false
300 */
301 // public static boolean isTime(String strTime) {
302 // try {
303 // parse(strTime, HH_MM_SS);
304 // return true;
305 // }
306 // catch (ParseException pe) {
307 // return false;
308 // }
309 // }
310
311 /**
312 * 判断给定字符串是否为特定格式日期时间(包括:年月日时分秒 yyyy-MM-dd hh:mm:ss)数据
313 *
314 * @param strDateTime
315 * 要判断的日期时间
316 *@return true 如果是,否则返回false
317 */
318 // public static boolean isDateTime(String strDateTime) {
319 // try {
320 // parse(strDateTime, YYYY_MM_DD_HH_MM_SS);
321 // return true;
322 // }
323 // catch (ParseException pe) {
324 // return false;
325 // }
326 // }
327
328 /**
329 * 获取一个简单的日期格式化对象
330 *
331 * @return 一个简单的日期格式化对象
332 */
333 private static SimpleDateFormat getFormatter(String parttern) {
334 return new SimpleDateFormat(parttern);
335 }
336
337 /**
338 * 获取给定日前的后intevalDay天的日期
339 *
340 * @param refenceDate
341 * 给定日期(格式为:yyyy-MM-dd)
342 * @param intevalDays
343 * 间隔天数
344 * @return 计算后的日期
345 */
346 public static String getNextDate(String refenceDate, int intevalDays) {
347 try {
348 return getNextDate(parse(refenceDate, YYYY_MM_DD), intevalDays);
349 } catch (Exception ee) {
350 return "";
351 }
352 }
353
354 /**
355 * 获取给定日前的后intevalDay天的日期
356 *
357 * @param refenceDate
358 * Date 给定日期
359 * @param intevalDays
360 * int 间隔天数
361 * @return String 计算后的日期
362 */
363 public static String getNextDate(Date refenceDate, int intevalDays) {
364 try {
365 java.util.Calendar calendar = java.util.Calendar.getInstance();
366 calendar.setTime(refenceDate);
367 calendar.set(Calendar.DATE, calendar.get(Calendar.DATE)
368 + intevalDays);
369 return format(calendar.getTime(), YYYY_MM_DD);
370 } catch (Exception ee) {
371 return "";
372 }
373 }
374
375 public static long getIntevalDays(String startDate, String endDate) {
376 try {
377 return getIntevalDays(parse(startDate, YYYY_MM_DD), parse(endDate,
378 YYYY_MM_DD));
379 } catch (Exception ee) {
380 return 0l;
381 }
382 }
383
384 public static long getIntevalDays(Date startDate, Date endDate) {
385 try {
386 java.util.Calendar startCalendar = java.util.Calendar.getInstance();
387 java.util.Calendar endCalendar = java.util.Calendar.getInstance();
388
389 startCalendar.setTime(startDate);
390 endCalendar.setTime(endDate);
391 long diff = endCalendar.getTimeInMillis()-startCalendar.getTimeInMillis();
392
393 return (diff / (1000 * 60 * 60 * 24));
394 } catch (Exception ee) {
395 return 0l;
396 }
397 }
398
399 /**
400 *求当前日期和指定字符串日期的相差天数
401 *
402 * @param startDate
403 * @return
404 */
405 public static long getTodayIntevalDays(String startDate) {
406 try {
407 // 当前时间
408 Date currentDate = new Date();
409
410 // 指定日期
411 SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
412 java.util.Date theDate = myFormatter.parse(startDate);
413
414 // 两个时间之间的天数
415 long days = (currentDate.getTime() - theDate.getTime())
416 / (24 * 60 * 60 * 1000);
417
418 return days;
419 } catch (Exception ee) {
420 return 0l;
421 }
422 }
423
424 public static Date parseToDate(String dateTimeStr) {
425 if (dateTimeStr == null)
426 return null;
427 Date d = null;
428 int formatStrLength = formatStr.length;
429 for (int i = 0; i < formatStrLength; i++) {
430 d = parseToDate2(dateTimeStr, formatStr[i]);
431 if (d != null) {
432 break;
433 }
434 }
435 return d;
436 }
437
438 private static Date parseToDate2(String dateTimeStr, String formatString) {
439 Date d = null;
440 SimpleDateFormat sdf = new SimpleDateFormat(formatString);
441 try {
442 d = sdf.parse(dateTimeStr);
443 } catch (ParseException pe) {
444
445 }
446 return d;
447 }
448
449 public static String dateTimeToString(Date datetime) {
450 // dateTime=dateTime.substring(0,4)+dateTime.substring(5,7)+dateTime.substring(8,10)+dateTime.substring(11,13)+dateTime.substring(14,16)+dateTime.substring(17,19);
451 // return dateTime;
452
453 java.util.GregorianCalendar calendar = new java.util.GregorianCalendar();
454 calendar.setTime(datetime);
455 String dateTime = calendar.get(Calendar.YEAR) + ""
456 + (calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0")
457 + (calendar.get(Calendar.MONTH) + 1) + ""
458 + (calendar.get(Calendar.DATE) > 9 ? "" : "0")
459 + calendar.get(Calendar.DATE) + ""
460 + (calendar.get(Calendar.HOUR_OF_DAY) > 9 ? "" : "0")
461 + calendar.get(Calendar.HOUR_OF_DAY) + ""
462 + (calendar.get(Calendar.MINUTE) > 9 ? "" : "0")
463 + calendar.get(Calendar.MINUTE) + ""
464 + (calendar.get(Calendar.SECOND) > 9 ? "" : "0")
465 + calendar.get(Calendar.SECOND);
466 return dateTime;
467 }
468
469 /**
470 * 由年、月份,获得当前月的最后一天
471 *
472 * @param year
473 * month 月份 01 02 11 12
474 * @return
475 * @throws ParseException
476 */
477 public static String getLastDayOfMonth(String year, String month)
478 throws ParseException {
479 String LastDay = "";
480 Calendar cal = Calendar.getInstance();
481 Date date_;
482 Date date = new SimpleDateFormat("yyyy-MM-dd").parse(year + "-" + month
483 + "-14");
484 cal.setTime(date);
485 int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
486 cal.set(Calendar.DAY_OF_MONTH, value);
487 date_ = cal.getTime();
488 LastDay = new SimpleDateFormat("yyyy-MM-dd").format(date_);
489 return LastDay;
490 }
491
492 /**
493 * debug
494 */
495 public static void main(String[] args) {
496 try {
497 System.out.println( "当前日期:" + DateUtils.getCurrDateStr());
498 System.out.println( "日期格式化:" + DateUtils.format(new Date(), DateUtils.formatStr_yyyyMMddHHmmssS));
499 System.out.println( "短日期:" + DateUtils.format(new Date()));
500 System.out.println( "长日期:" + DateUtils.getCurrDateTimeStr());
501 System.out.println( "日:" + DateUtils.getDay());
502 System.out.println( "月:" + DateUtils.getMonth());
503 System.out.println( "年:" + DateUtils.getYear());
504 System.out.println( "月未最后一天:" + DateUtils.getLastDayOfMonth("2010", "08"));
505 System.out.println( "相差几天:" + DateUtils.getIntevalDays("2010-08-01", "2010-08-21"));
506
507 System.out.println( "当前日期后的几天:" + DateUtils.getNextDate("2010-08-01", -3));
508 System.out.println( "与今天相差几天:" + DateUtils.getTodayIntevalDays("2010-08-01"));
509 } catch (Exception e) {
510 e.printStackTrace();
511 }
512 }
513 }
514
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: