DateUtils.java 15.2 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 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
package com.ectrip.cyt.utils;

import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * 实现描述:时间操作工具类
 *
 * @author zixin.zhang
 * @version v1.0.0
 * @see
 * @since 13-8-12上午10:37
 */
public class DateUtils {
	public final static String DATE_CHINESE_PATTERN = "yyyy-MM-dd";
	/**
	 * 标准日期格式
	 */
	public final static String DATE_PATTERN = "yyyy-MM-dd";

	public final static String DATE_SHORT_PATTERN = "yyyyMMdd";

	public final static String DATE_SLASH_PATTERN = "yyyy/MM/dd";

	/**
	 * 标准日期时分秒毫秒格式
	 */
	public final static String DATETIME_MILL_SECOND = "yyyy-MM-dd HH:mm:ss.SSS";

	/**
	 * 标准时间格式
	 */
	public final static String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 特殊的格式 针对创建订单,拼凑的最晚支付时间
	 */
	public final static String DATETIME_PATTERN_CREAT_ORDER = "yyyy-MM-dd HH:mm";

	public final static String DATETIME_SHORT_PATTERN = "yyyyMMddHHmmss";

	/**
	 * 标准年小时分钟格式
	 */
	public final static String HOUR_MINUTE = "HH:mm";

	/**
	 * 标准年小时分钟秒格式
	 */
	public final static String HOUR_MINUTE_SECOND = "HH:mm:ss";

	/**
	 * Number of milliseconds in a standard day.
	 */
	public static final long MILLIS_PER_DAY = 24 * DateUtils.MILLIS_PER_HOUR;

	/**
	 * Number of milliseconds in a standard hour.
	 */
	public static final long MILLIS_PER_HOUR = 60 * DateUtils.MILLIS_PER_MINUTE;

	/**
	 * Number of milliseconds in a standard minute.
	 */
	public static final long MILLIS_PER_MINUTE = 60 * DateUtils.MILLIS_PER_SECOND;

	/**
	 * Number of milliseconds in a standard second.
	 */
	public static final long MILLIS_PER_SECOND = 1000;

	/**
	 * 标准年月格式
	 */
	public final static String MONTH_PATTERN = "yyyy-MM";

	private final static String[] WEEK_NAMES = { "星期一", "星期二", "星期三", "星期四",
			"星期五", "星期六", "星期天" };

	/**
	 * 在指定日期增加:天数
	 *
	 * @param date
	 *            指定日期
	 * @param days
	 *            指定天数
	 * @return
	 */
	public static Date addDay(Date date, int days) {
		if (days == 0) {
			return date;
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DAY_OF_YEAR, days);
		return c.getTime();
	}

	/**
	 * 获取当前时间 格式: String
	 */
	public static String getTodayStr() {
		String nowString = "";
		String monthString = "";
		String dayString = "";
		String hourString = "";
		String minString = "";
		String secString = "";

		Calendar now = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
		nowString = nowString + now.get(1) + "-";
		Integer month = now.get(2) + 1;
		if (month < 10) {
			monthString = "0" + month;
		} else {
			monthString = month.toString();
		}

		Integer day = now.get(5);
		if (day < 10) {
			dayString = "0" + day;
		} else {
			dayString = day.toString();
		}

		nowString = nowString + monthString + "-" + dayString;

		Integer hour = now.get(11);
		if (hour < 10) {
			hourString = "0" + hour;
		} else {
			hourString = hour.toString();
		}

		nowString = nowString + " " + hourString;
		Integer min = now.get(12);
		if (min < 10) {
			minString = "0" + min;
		} else {
			minString = min.toString();
		}

		nowString = nowString + ":" + minString;
		Integer sec = now.get(13);
		if (sec < 10) {
			secString = "0" + sec;
		} else {
			secString = sec.toString();
		}

		nowString = nowString + ":" + secString;
		return nowString;
	}

	/**
	 * 获取当前时间 格式: Date
	 */
	public static Date getTodayDate() {
		return convertDate(getTodayStr(), DATETIME_PATTERN);
	}

	/**
	 * 获取当前时间 : 根据规范格式
	 */
	public static Date getTodayDate(String pattern) {
		return convertDate(getTodayStr(), pattern);
	}

	/**
	 * 获取当前: 时分秒
	 */
	public static String getTodayTime() {
		return getTodayStr().substring(11);
	}

	/**
	 * 在指定日期增加:小时
	 *
	 * @param date
	 *            指定日期
	 * @param days
	 *            指定天数
	 * @return
	 */
	public static Date addHour(Date date, int hour) {
		if (hour == 0) {
			return date;
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.HOUR, hour);
		return c.getTime();
	}

	/**
	 * 在指定日期增加指定天数
	 *
	 * @param date
	 *            指定日期
	 * @param days
	 *            指定天数
	 * @return
	 */
	public static Date addDay(String date, int days) {
		return DateUtils.addDay(DateUtils.convertDate(date), days);
	}

	public static Date addMinute(Date date, int minute) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MINUTE, minute);
		return c.getTime();
	}

	/**
	 * 当前日期之后
	 *
	 * @param date
	 * @return
	 */
	public static boolean afterToday(Object date) {
		Date currentDate = getTodayDate();
		return DateUtils.compareDate(date, currentDate) == 1;
	}

	/**
	 * 当前时间之后
	 *
	 * @param date
	 * @return
	 */
	public static boolean afterTodayDate(Date date) {
		Date currentDate = getTodayDate();
		return currentDate.compareTo(date) == -1;
	}

	/**
	 * 当前日期之前
	 *
	 * @param date
	 * @return
	 */
	public static boolean beforeToday(Object date) {
		Date currentDate = getTodayDate();
		return DateUtils.compareDate(date, currentDate) == -1;
	}

	/**
	 * 当前时间之前
	 *
	 * @param date
	 * @return
	 */
	public static boolean beforeTodayDate(Date date) {
		Date currentDate = getTodayDate();
		return currentDate.compareTo(date) == 1;
	}

	/**
	 * 比较两个日期date1大于date2 返回1 等于返回0 小于返回-1
	 *
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int compareDate(Object date1, Object date2) {
		if (date1 == null || date2 == null) {
			String msg = "illegal arguments,date1 and date2 must be not null.";
			throw new IllegalArgumentException(msg);
		}
		Date d1 = (Date) (date1 instanceof String ? DateUtils
				.convertDate((String) date1) : date1);
		Date d2 = (Date) (date2 instanceof String ? DateUtils
				.convertDate((String) date2) : date2);
		try {
			return d1.compareTo(d2);
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	public static Date convertDate(Date date, String pattern) {
		if (pattern==null) {
			String msg = "the date or pattern is empty.";
			throw new IllegalArgumentException(msg);
		}
		String dateForPattern = DateUtils.formatDate(date, pattern);
		return DateUtils.convertDate(dateForPattern, pattern);
	}

	/**
	 * 将long型整数转化为时间。
	 *
	 * @param date
	 *            时间对应的long值
	 * @return 时间对象
	 */
	public static Date convertDate(Long date) {
		return new Date(date);
	}

	/**
	 * 将日期或者时间戳转化为日期对象
	 *
	 * @param date
	 *            yyyy-MM-dd or yyyy-MM-dd HH:mm:ss or yyyy-MM-dd HH:mm:ss.SSS
	 * @return
	 */
	public static Date convertDate(String date) {
		if (date.indexOf(":") > 0) {
			return DateUtils.convertDate(date, DateUtils.DATETIME_PATTERN);
		} else if (date.indexOf(".") > 0) {
			return DateUtils.convertDate(date, DateUtils.DATETIME_MILL_SECOND);
		} else {
			return DateUtils.convertDate(date, DateUtils.DATE_PATTERN);
		}
	}

	/**
	 * 将日期或者时间字符串转化为日期对象
	 *
	 * @param date
	 *            日期字符串
	 * @param pattern
	 *            格式字符串</br> yyyy-MM-DD, yyyy/MM/DD, yyyyMMdd</br>
	 *            yyyy-MM-dd-HH:mm:ss, yyyy-MM-dd HH:mm:ss
	 *            格式字符串可选字符:"GyMdkHmsSEDFwWahKzZ"
	 * @return Date
	 * @see java.text.DateFormatSymbols.patternChars</br>
	 */
	public static Date convertDate(String date, String pattern) {
		try {
			if (pattern==null) {
				String msg = "the date or pattern is empty.";
				throw new IllegalArgumentException(msg);
			}
			SimpleDateFormat df = new SimpleDateFormat(pattern.trim());
			return df.parse(date.trim());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 将时间字符串转化为时间对象Time
	 *
	 * @param time
	 *            时间字符串
	 * @param pattern
	 *            格式字符串 yyyy-MM-dd HH:mm:ss or yyyy-MM-dd HH:mm:ss.SSS
	 * @return
	 */
	public static Time convertTime(String time, String pattern) {
		Date d = DateUtils.convertDate(time, pattern);
		return new Time(d.getTime());
	}

	/**
	 * 获得日期相差天数
	 *
	 * @param date1
	 *            日期
	 * @param date2
	 *            日期
	 * @return
	 */
	public static int diffDate(Date date1, Date date2) {
		return (int) ((date1.getTime() - date2.getTime()) / DateUtils.MILLIS_PER_DAY);
	}

	/**
	 * 获取两个日期相差的分钟数
	 *
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int diffMinute(Date date1, Date date2) {
		return (int) ((date1.getTime() - date2.getTime()) / DateUtils.MILLIS_PER_MINUTE);
	}

	/**
	 * 格式为时间字符串
	 *
	 * @param date
	 *            日期
	 * @return yyyy-MM-dd Date
	 */
	public static String formatDate(Date date) {
		try {
			return DateUtils.formatDate(date, DateUtils.DATE_PATTERN);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 按指定格式字符串格式时间
	 *
	 * @param date
	 *            日期或者时间
	 * @param pattern
	 *            格式化字符串 yyyy-MM-dd, yyyy-MM-dd HH:mm:ss, yyyy年MM月dd日 etc.</br>
	 * @return
	 */
	public static String formatDate(Date date, String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern.trim());
		return format.format(date);
	}

	/**
	 * 格式为时间戳字符串
	 *
	 * @param date
	 *            时间
	 * @return yyyy-MM-dd HH:mm:ss Date
	 */
	public static String formatDateTime(Date date) {
		try {
			return DateUtils.formatDate(date, DateUtils.DATETIME_PATTERN);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 将制定时间格式为字符串'yyyyMMddHHmmss'.
	 *
	 * @return
	 */
	public static String formatDateToYMDHMS(Date date) {
		return DateUtils.formatDate(date, DateUtils.DATETIME_SHORT_PATTERN);
	}

	public static String formatMonth(Date date) {
		return DateUtils.formatDate(date, DateUtils.MONTH_PATTERN);
	}

	/**
	 * 将当前时间格式为字符串'yyyyMMddHHmmss'.
	 *
	 * @return
	 */
	public static String formatNowToYMDHMS() {
		return DateUtils.formatDateToYMDHMS(getTodayDate());
	}

	public static Timestamp getCurrentTimestamp() {
		return new Timestamp(getTodayDate().getTime());
	}

	public static Date getDateFromShortString(String str) {
		SimpleDateFormat simpleDF = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return simpleDF.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获得本周第一天
	 *
	 * @param date
	 * @return
	 */
	public static Date getFirstDayOfThisWeek(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return cal.getTime();
	}

	/**
	 * 获得小时
	 *
	 * @param date
	 * @return
	 */
	public static int getHourOfDay(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.HOUR_OF_DAY);
	}

	public static Date getLastMonth() {
		Calendar c = Calendar.getInstance();
		int month = c.get(Calendar.MONTH);
		c.set(Calendar.MONTH, month - 1);
		return c.getTime();
	}

	/**
	 * 获得分钟数
	 *
	 * @param date
	 * @return
	 */
	public static int getMinute(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.MINUTE);
	}

	/**
	 * 获取后续第n天日期
	 *
	 * @param date
	 * @param n
	 *            第n天
	 * @return
	 */
	public static Date getNextNDay(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, n);
		return cal.getTime();
	}

	/**
	 * 获得星期数
	 *
	 * @param date
	 *            日期
	 * @return
	 */
	public static int getWeekNumber(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int number = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (number == 0) {
			number = 7;
		}
		return number;
	}

	/**
	 * 获得星期名称
	 *
	 * @param date
	 * @return
	 */
	public static String getWeekNumberString(Date date) {
		int dayNum = DateUtils.getWeekNumber(date);
		return DateUtils.WEEK_NAMES[dayNum - 1];
	}

	/**
	 * 是否同一天
	 *
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean isSameDay(Object date1, Object date2) {
		return DateUtils.compareDate(date1, date2) == 0;
	}

	/**
	 * 检查时间或者字符串是否合法
	 *
	 * @param date
	 *            时间
	 * @param pattern
	 *            格式串
	 * @return
	 */
	public static boolean isValidDate(String date, String pattern) {
		try {
			DateUtils.convertDate(pattern, date);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 获得当前时间戳
	 *
	 * @return Timestamp
	 */
	public static Timestamp now() {
		return new Timestamp(getTodayDate().getTime());
	}

	/**
	 * 获得当前时间字符串,格式为:yyyy-MM-dd HH:mm:ss
	 *
	 * @return
	 */
	public static String nowDateTime() {
		return DateUtils.formatDate(getTodayDate(), DateUtils.DATETIME_PATTERN);
	}

	/**
	 * 按指定roundType格式化日期。
	 *
	 * @param date
	 *            日期
	 * @param roundType
	 * @return Date
	 * @see Calendar.MONTH
	 *      ,Calendar.DATE,Calendar.HOUR,Calendar.MINUTE,Calendar.SECOND
	 */
	public static Date round(Date date, int roundType) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(date.getTime());
		switch (roundType) {
			case Calendar.MONTH:
				c.set(Calendar.DAY_OF_MONTH, 1);
			case Calendar.DATE:
				c.set(Calendar.HOUR_OF_DAY, 0);
			case Calendar.HOUR:
				c.set(Calendar.MINUTE, 0);
			case Calendar.MINUTE:
				c.set(Calendar.SECOND, 0);
			case Calendar.SECOND:
				c.set(Calendar.MILLISECOND, 0);
				return c.getTime();
			default:
				throw new IllegalArgumentException("invalid round roundType.");
		}
	}

	/**
	 * 获得当前日期对象
	 *
	 * @return
	 */
	public static Date today() {
		return DateUtils.convertDate(DateUtils.formatDate(getTodayDate()),
				DateUtils.DATE_PATTERN);
	}

	/**
	 * 获得当前日期字符串,格式为:yyyy-MM-dd
	 *
	 * @return
	 */
	public static String todayDate() {
		return DateUtils.formatDate(getTodayDate());
	}

	/**
	 *
	 * 将日期或者时间字符串转化为Timestamp对象
	 *
	 * @param date
	 *            日期字符串
	 * @param pattern
	 *            格式字符串</br> yyyy-MM-DD, yyyy/MM/DD, yyyyMMdd</br>
	 *            yyyy-MM-dd-HH:mm:ss, yyyy-MM-dd HH:mm:ss
	 * @return Timestamp
	 * @author reeboo
	 */
	public static Timestamp toTimestamp(String date, String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern.trim());
		try {
			return new Timestamp(format.parse(date).getTime());
		} catch (ParseException e) {
		}
		return null;
	}

	/**
	 * 获得当天日期 Describe:
	 *
	 * @author:hezhihong
	 * @return return:String 日期 yyyy-MM-dd Date:2014-5-26
	 */
	public static String getDays() {
		return getTodayStr().substring(0, 10);
	}

	/**
	 * 获取当前的时间 yyyy-MM-dd
	 * @return
	 */
	public static String getCurrentDate() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
		Date date = new Date(System.currentTimeMillis());
		return format.format(date);
	}
}