DateUtils.java 35.8 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 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
package com.erry.iclear.dateInterface.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

/**
 * @author Administrator
 */
@Slf4j
public class DateUtils {

	private DateUtils() {
	};

	
	public static String YEAR_MONTH = "yyyyMM";
	public static String SHORT_DATE = "yyyyMMdd";

	/**
	 * 功能:计算两个日期之间相差多少天 ((date2-date1)后取整)
	 * 
	 * @param date1 减日期
	 * @param date2 被减日期
	 * @return
	 */
	public static int dateDiff(Date date1, Date date2) {
		Calendar calendar1 = Calendar.getInstance();
		Calendar calendar2 = Calendar.getInstance();
		calendar1.setTime(date1);
		calendar2.setTime(date2);
		calendar1.set(Calendar.HOUR_OF_DAY, 0);
		calendar1.set(Calendar.MINUTE, 0);
		calendar1.set(Calendar.SECOND, 0);
		calendar1.set(Calendar.MILLISECOND, 0);
		
		calendar2.set(Calendar.HOUR_OF_DAY, 0);
		calendar2.set(Calendar.MINUTE, 0);
		calendar2.set(Calendar.SECOND, 0);
		calendar2.set(Calendar.MILLISECOND, 0);
		
		long l = Math.abs(calendar2.getTimeInMillis() - calendar1.getTimeInMillis());
		long increaseDate = l / 1000 / 60 / 60 / 24;
		if ((l % (1000 * 60 * 60 * 24)) > 0) {
			increaseDate = increaseDate + 1;
		}
		return (int) increaseDate;
	}
	
	/**
	 * date2 是否是date1 的下一天
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean isNextDate(Date date1, Date date2) {
	    if (date2.after(date1) && dateDiff(date1,date2) ==1) {
	    	return true;
	    }
		return false;
	}

	/**
	 * 功能:按yyyy-MM-dd HH:mm:ss 或 yyyy-MM-dd转换为日期
	 * 
	 * @param s
	 * @return
	 * @throws Exception
	 */
	public static Date parseToDate(String s) throws Exception {
		DateFormat df;
		if (s == null) {
			return null;
		}
		if (s.contains(":")) {
			try {
				df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				return df.parse(s);
			} catch (Exception e) {
				log.error(e.getMessage());
				throw e;
			}
		} else {
			try {
				df = new SimpleDateFormat("yyyy-MM-dd");
				return df.parse(s);
			} catch (Exception e) {
				log.error(e.getMessage());
				throw e;
			}
		}
	}

	/**
	 * date1 > date2+before-->true
	 * 
	 * @param date1
	 * @param date2
	 * @param before
	 * @return
	 */
	public static boolean isOverTime(Date date1, Date date2, int before) {
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date2);
			calendar.add(Calendar.HOUR, -1 * before);
			// 如果参数 Date 等于此 Date,则返回值 0;
			// 如果此 Date 在 Date 参数之前,则返回小于 0 的值;
			// 如果此 Date 在 Date 参数之后,则返回大于 0 的值。
			if (date1.compareTo(calendar.getTime()) > 0) {
				return true;
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return false;
	}

	/**
	 * 功能:判断时间是否是凌晨(0-6),默认当前系统时间
	 * 
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static boolean isDawn(Date date) throws Exception {
		boolean isDawn = false;
		Calendar tempCal = Calendar.getInstance();
		if (date != null) {
			tempCal.setTime(date);
		}
		int hour = tempCal.get(Calendar.HOUR_OF_DAY);
		if (0 <= hour && hour <= 6) {
			isDawn = true;
		}
		return isDawn;
	}

	/**
	 * 功能:根据传入的字符串,得到holdTime,得到的时间为1970年的HH:mm:ss
	 * 如果格式不对(不为例如:23:12:23或着23:56或着23),抛出异常
	 * 
	 * @param str
	 * @return
	 */
	public static Date getHoldTimeDate(String str) throws Exception {
		boolean flag = false;
		Pattern p1 = Pattern.compile("(0\\d{1}|1\\d{1}|2[0-3]):[0-5]\\d{1}:([0-5]\\d{1})");
		Matcher m1 = p1.matcher(str);
		Pattern p2 = Pattern.compile("(0\\d{1}|1\\d{1}|2[0-3])");
		Matcher m2 = p2.matcher(str);
		Pattern p3 = Pattern.compile("(0\\d{1}|1\\d{1}|2[0-4]):[0-5]\\d{1}");
		Matcher m3 = p3.matcher(str);
		if (m1.matches() || m2.matches() || m3.matches()) {
			flag = true;
			if (m3.matches()) {
				return string2Date(str, "HH:mm");
			}
			if (m1.matches()) {
				return string2Date(str, "HH:mm:ss");
			}

			if (m2.matches()) {
				return string2Date(str, "HH");
			}
		}

		if (!flag) {
			throw new RuntimeException("holdTime format is invalid!");
		}
		return null;
	}

	/**
	 * 日期转为HoldTime格式HH:mm
	 * 
	 * @param date
	 * @return
	 */
	public static String date2HoldTime(Date date) {
		if (date == null){
			return "";
		}
		SimpleDateFormat dateformat = new SimpleDateFormat("HH:mm");
		return dateformat.format(date);
	}

	/**
	 * 功能:算出两个日期中所包含的月份
	 * 
	 * @param fromDate
	 * @param toDate
	 * @return
	 */
	public static Set<String> getMonthBetweenTwoDate(Date fromDate, Date toDate) {
		long begin = fromDate.getTime();
		long end = toDate.getTime();
		long inter = end - begin;
		if (inter < 0) {
			inter = inter * (-1);
		}
		long dateMillSec = 86400000;
		long dateCnt = inter / dateMillSec;
		long remainder = inter % dateMillSec;
		if (remainder != 0) {
			dateCnt++;
		}
		Set<String> set = new LinkedHashSet<String>();
		Calendar cl = Calendar.getInstance();
		cl.setTime(fromDate);
		cl.set(Calendar.HOUR_OF_DAY, 0);
		cl.set(Calendar.MINUTE, 0);
		cl.set(Calendar.SECOND, 0);
		cl.set(Calendar.MILLISECOND, 0);
		set.add(getDateyyyyMM(cl.getTime()));
		for (int i = 1; i <= dateCnt; i++) {
			cl.add(Calendar.DAY_OF_YEAR, 1);
			set.add(getDateyyyyMM(cl.getTime()));
		}
		return set;
	}

	/**
	 * 功能:获取yyyyMM的年月
	 * 
	 * @param date
	 * @return
	 */
	public static String getDateyyyyMM(Date date) {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
		return df.format(date);
	}

	/**
	 * 功能:获取yyyyMMdd的年月日
	 * 
	 * @param date
	 * @return
	 */
	public static String getDateyyyyMMdd(Date date) {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		return df.format(date);
	}

	/**
	 * 功能:获取yyyyMM的年月
	 * 
	 * @param month
	 * @return
	 */
	public static Date parseDateyyyyMM(String month) {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
		try {
			return df.parse(month);
		} catch (ParseException e) {
		}
		return null;
	}

	/**
	 * 功能:格式yyyyMMdd日期
	 * 
	 * 
	 * @param date
	 * @return
	 */
	public static Date parseDateyyyyMMdd(String date) {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		try {
			return df.parse(date);
		} catch (ParseException e) {
		}
		return null;
	}

	/**
	 * 功能:根据两个日期,算出某个月份的第一天,或者最后一天
	 * 
	 * @param dateFrom
	 * @param dateTo
	 * @param month
	 * @param flag start
	 * @return
	 */
	public static int getDayBetweenDateStartOrEnd(Date dateFrom, Date dateTo, Date month, String flag) {
		if (dateFrom.getTime() > dateTo.getTime()) {
			Date temp = dateFrom;
			dateFrom = dateTo;
			dateTo = temp;
		}
		if (getDateyyyyMM(month).compareTo(getDateyyyyMM(dateFrom)) > 0
		        && getDateyyyyMM(month).compareTo(getDateyyyyMM(dateTo)) < 0) {
			if ("start".equals(flag)){
				return 1;
			}

			return getMonthsMaxDay(month);
		} else if (getDateyyyyMM(month).compareTo(getDateyyyyMM(dateFrom)) == 0
		        && getDateyyyyMM(month).compareTo(getDateyyyyMM(dateTo)) < 0) {
			if ("start".equals(flag)){
				return getDayOfMonth(dateFrom);
			}

			return getMonthsMaxDay(month);
		} else if (getDateyyyyMM(month).compareTo(getDateyyyyMM(dateFrom)) > 0
		        && getDateyyyyMM(month).compareTo(getDateyyyyMM(dateTo)) == 0) {
			if ("start".equals(flag)){
				return 1;
			}
			return getDayOfMonth(dateTo);
		} else {
			if ("start".equals(flag)) {
				return getDayOfMonth(dateFrom);
			}
			return getDayOfMonth(dateTo);
		}

	}

	/**
	 * 功能:根据一个日期,算出是这个月中的第几天
	 * 
	 * @param date
	 * @return
	 */
	public static int getDayOfMonth(Date date) {
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date);
		return cal1.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 功能:取出一个月中某天的日期
	 * 
	 * @param month
	 * @param num
	 * @return
	 */
	public static Date getDateOfMonthByNum(String month, int num) {
		Calendar cl = Calendar.getInstance();
		cl.setTime(parseDateyyyyMM(month));
		cl.set(Calendar.HOUR_OF_DAY, 0);
		cl.set(Calendar.MINUTE, 0);
		cl.set(Calendar.SECOND, 0);
		cl.set(Calendar.MILLISECOND, 0);
		cl.add(Calendar.DAY_OF_YEAR, num - 1);
		return cl.getTime();
	}

	/**
	 * 功能:得到本周第一天日期
	 * 
	 * @return
	 */
	public static Date getCurrentWeekMonday() {

		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		int index = cal.get(Calendar.DAY_OF_WEEK);
		// 转成中国的习惯,如果是第一天,则转化为第七天(星期天外国为一周的第一天,而中国为每周的最后一天)
		if (index == 1){
			index = 8;
		}

		cal.add(Calendar.DATE, -(index - 2));
		return cal.getTime();

	}

	/**
	 * 功能:得到本周最后一天的日期
	 * 
	 * @return
	 */
	public static Date getCurrentWeekSaturday() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		int index = cal.get(Calendar.DAY_OF_WEEK);
		if (index == 1){
			index = 8;
		}

		cal.add(Calendar.DATE, -(index - 2));
		cal.add(Calendar.DATE, 6);
		return cal.getTime();
	}

	/**
	 * 功能:从指定日期移动一定的天数
	 * 
	 * @param date
	 * @param day
	 * @return
	 */
	public static Date moveDay(Date date, int day) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		cal.add(Calendar.DAY_OF_MONTH, day);
		return cal.getTime();
	}

	/**
	 * 功能:从当天日期移动一定的月数
	 * 
	 * @param month
	 * @return
	 */

	public static Date getMoveMonthDate(int month) {
		Date nowDate = new Date();
		Calendar cl = Calendar.getInstance();
		cl.setTime(nowDate);
		cl.add(Calendar.MONDAY, month - 1);
		Date date1 = cl.getTime();
		return date1;
	}

	/**
	 * 功能:获取昨天
	 * 
	 * @return
	 */
	public static Date getYesterday() {
		Calendar cal = Calendar.getInstance();

		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		cal.add(Calendar.DAY_OF_MONTH, -1);
		return cal.getTime();
	}

	/**
	 * 功能:根据某个日期,返回本月第一天
	 * 
	 * @param date 任何一天
	 * @return Date 当月第一天
	 */
	public static Date getMonthsFirstDay(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 功能:根据某个日期,返回本月最后一天
	 * 
	 * @param date 任何一天
	 * @return Date 当月第一天
	 */
	public static Date getMonthsLastDay(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		cal.add(Calendar.MONTH, 1);
		cal.add(Calendar.DATE, -1);
		return cal.getTime();
	}

	/**
	 * 功能:获取起始日期和终止日期之间的日期(包含起始和终止日期)
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static Set<String> getDayList(Date startDate, Date endDate) {
		long begin = startDate.getTime();
		long end = endDate.getTime();
		long inter = end - begin;
		if (inter < 0) {
			inter = inter * (-1);
		}
		long dateMillSec = 86400000;
		long dateCnt = inter / dateMillSec;
		Set<String> set = new LinkedHashSet<String>();
		Calendar cl = Calendar.getInstance();
		cl.setTime(startDate);
		cl.set(Calendar.HOUR_OF_DAY, 0);
		cl.set(Calendar.MINUTE, 0);
		cl.set(Calendar.SECOND, 0);
		cl.set(Calendar.MILLISECOND, 0);
		set.add(getDateyyyyMMdd(cl.getTime()));
		for (int i = 1; i <= dateCnt; i++) {
			cl.add(Calendar.DAY_OF_YEAR, 1);
			set.add(getDateyyyyMMdd(cl.getTime()));
		}
		set.add(getDateyyyyMMdd(endDate));
		return set;
	}

	/**
	 * 功能:取得两个日期中最小的日期,如果两个日期参数都为null则返回null
	 * 
	 * @param date1
	 * @param date2
	 * @return Date or null
	 */
	public static Date getMinimizeDate(Date date1, Date date2) {
		if (date1 == null) {
			return date2;
		}
		if (date2 == null) {
			return date1;
		}
		if (date1.compareTo(date2) > 0) {
			return date2;
		}
		return date1;
	}

	/**
	 * 功能:取得两个日期中最大的日期,如果两个日期参数都为null则返回null
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static Date getMaxmizeDate(Date date1, Date date2) {
		if (date1 == null) {
			return date2;
		}
		if (date2 == null) {
			return date1;
		}
		if (date1.compareTo(date2) < 0) {
			return date2;
		}
		return date1;
	}

	/**
	 * 功能:获取指定日期和天数的星期几
	 * 
	 * @param date 指定日期
	 * @param day 指定天数
	 * @return
	 */
	public static int getDayofWeek(Date date, int day) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + day - 1);
		return calendar.get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * 功能:判断两个Date是否为一天
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean isSameDay(Date date1, Date date2) {
		try {
			return org.apache.commons.lang3.time.DateUtils.isSameDay(date1, date2);
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 功能:把date1的小时,分钟,秒换成date2的小时,分钟,秒,返回换值后的date1
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static Date changeTheHourMinuteSecond(Date date1, Date date2) {
		Calendar cl1 = Calendar.getInstance();
		cl1.setTime(date1);
		cl1.set(Calendar.HOUR_OF_DAY, 0);
		cl1.set(Calendar.MINUTE, 0);
		cl1.set(Calendar.SECOND, 0);

		Calendar cl2 = Calendar.getInstance();
		cl2.setTime(date2);
		cl1.set(Calendar.HOUR_OF_DAY, cl2.get(Calendar.HOUR_OF_DAY));
		cl1.set(Calendar.MINUTE, cl2.get(Calendar.MINUTE));
		cl1.set(Calendar.SECOND, cl2.get(Calendar.SECOND));

		return cl1.getTime();
	}
	
	/**
	 * 功能:把date1的小时,分钟,秒换成23:59:59
	 * 
	 * @param date1
	 * @return yyyy-MM-dd 23:59:59
	 */
	public static Date getDayOfMaxHourMinuteSecond(Date date1)  {
		Calendar cl1 = Calendar.getInstance();
		cl1.setTime(date1);
		cl1.set(Calendar.HOUR_OF_DAY, 23);
		cl1.set(Calendar.MINUTE, 59);
		cl1.set(Calendar.SECOND, 59);

		return cl1.getTime();
	}

	/**
	 * 功能:添加天数
	 * 
	 * @param basicDate
	 * @param days 正、负 整数 或0
	 * @return
	 */
	public static String addDays(String basicDate, int days) {
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(java.sql.Date.valueOf(basicDate));
			cal.add(GregorianCalendar.DATE, days);
			return formatDate(cal.getTime(), "yyyy-MM-dd");
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 功能:对日期进行格式化输出
	 * 
	 * @param date 日期
	 * @param format
	 * @return
	 */
	public static String formatDate(Date date, String format) {
		try {
			if (format != null && !"".equals(format) && date != null) {
				SimpleDateFormat formatter = new SimpleDateFormat(format);
				return formatter.format(date);
			}
		} catch (Exception e) {
			return "";
		}
		return "";
	}

	/**
	 * 功能:获取两个日期之间的天数
	 * 
	 * @param dateFrom 起始日期
	 * @param dateEnd 结束日期
	 * @return
	 */
	public static long getDaysBetweenTwoDatesMath(String dateFrom, String dateEnd) {
		Date dtFrom = null;
		Date dtEnd = null;
		dtFrom = string2Date(dateFrom, "yyyy-MM-dd");
		dtEnd = string2Date(dateEnd, "yyyy-MM-dd");
		long begin = dtFrom.getTime();
		long end = dtEnd.getTime();
		long inter = end - begin;
		long dateMillSec = 24 * 60 * 60 * 1000;
		long dateCnt = inter / dateMillSec;
		long remainder = inter % dateMillSec;
		if (remainder != 0) {
			dateCnt++;
		}
		return dateCnt;
	}

	/**
	 * 功能:把日期字符串格式化为日期类型
	 * 
	 * @param datetext 日期字符串
	 * @param format 日期格式,如果不传则使用"yyyy-MM-dd HH:mm:ss"格式
	 * @return
	 */
	public static Date string2Date(String datetext, String format) {
		try {
			SimpleDateFormat df;
			if (datetext == null || "".equals(datetext.trim())) {
				return new Date();
			}
			if (format != null) {
				df = new SimpleDateFormat(format);
			} else {
				datetext = datetext.replaceAll("/", "-");
				df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			}
			Date date = df.parse(datetext);

			return date;

		}

		catch (Exception e) {
			e.printStackTrace();
			return new Date();
		}
	}

	/**
	 * 功能:取得两天之间的日期数组,包含开始日期与结束日期
	 * 
	 * @param startDateStr 开始日期
	 * @param endDateStr 结束日期
	 * @return Date[] 日期数组
	 */
	public static Date[] getBetweenTwoDayArray(String startDateStr, String endDateStr) {
		Date startDate = string2Date(startDateStr, "yyyy-MM-dd");
		int dateNum = Integer.parseInt(getDaysBetweenTwoDates(startDateStr, endDateStr)) + 1;
		Date[] dataArray = new Date[dateNum];
		for (int i = 0; i < dateNum; i++) {
			dataArray[i] = startDate;
			startDate = org.apache.commons.lang3.time.DateUtils.addDays(startDate, 1);
		}
		return dataArray;
	}

	/**
	 * 功能:获取两日期之间的天数
	 * 
	 * @param dateFrom 起始日期
	 * @param dateEnd 结束日期
	 * @return
	 */
	public static String getDaysBetweenTwoDates(String dateFrom, String dateEnd) {
		Date dtFrom = null;
		Date dtEnd = null;
		dtFrom = string2Date(dateFrom, "yyyy-MM-dd");
		dtEnd = string2Date(dateEnd, "yyyy-MM-dd");
		long begin = dtFrom.getTime();
		long end = dtEnd.getTime();
		long inter = end - begin;
		if (inter < 0) {
			inter = inter * (-1);
		}
		long dateMillSec = 24 * 60 * 60 * 1000;
		long dateCnt = inter / dateMillSec;
		long remainder = inter % dateMillSec;

		if (remainder != 0) {
			dateCnt++;
		}
		return String.valueOf(dateCnt);
	}

	/**
	 * 功能: 待确定
	 * 
	 * @param startDateStr
	 * @param endDateStr
	 * @return
	 */
	public static String[] getBetweenTwoDayMonthArray(String startDateStr, String endDateStr) {
		Date[] dateArray = getBetweenTwoDayArray(startDateStr, endDateStr);

		String monthFirstDay = formatDateYyyyMmDd(dateArray[0]).substring(2, 7);
		List<String> dayList = new ArrayList<String>();
		dayList.add(monthFirstDay);
		for (int i = 0; i < dateArray.length; i++) {
			if (!monthFirstDay.equals(formatDateYyyyMmDd(dateArray[i]).substring(2, 7))) {
				monthFirstDay = formatDateYyyyMmDd(dateArray[i]).substring(2, 7);
				dayList.add(monthFirstDay);
			}
		}
		String[] strArray = new String[dayList.size()];
		for (int i = 0; i < strArray.length; i++) {
			strArray[i] = dayList.get(i).substring(0, 2) + dayList.get(i).substring(3, 5);
		}

		return strArray;
	}

	/**
	 * 功能:获取星期几 SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and
	 * SATURDAY
	 * 
	 * @param date
	 * @return
	 */
	public static int getWeekByDate(Date date) {
		Calendar cl1 = Calendar.getInstance();
		cl1.setTime(date);
		cl1.set(Calendar.HOUR_OF_DAY, 0);
		cl1.set(Calendar.MINUTE, 0);
		cl1.set(Calendar.SECOND, 0);
		return cl1.get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * 功能:字符串转换为日期
	 * 
	 * @param datetext yyyy/MM/dd
	 * @return
	 */
	public static Date formatDateYyyyMmDd(String datetext) {
		try {
			SimpleDateFormat df;
			if (datetext == null || "".equals(datetext.trim())) {
				return null;
			}
			datetext = datetext.replaceAll("/", "-");
			df = new SimpleDateFormat("yyyy-MM-dd");
			Date date = df.parse(datetext);
			return date;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 功能: 添加Minute
	 * 
	 * @param cdate
	 * @param amount
	 * @return
	 */
	public static Date addMinute(Date cdate, int amount) {
		Calendar cl = Calendar.getInstance();
		cl.setTime(cdate);
		cl.add(Calendar.MINUTE, amount);
		return cl.getTime();
	}
	
	/**
	 * 功能: 添加Hour
	 * 
	 * @param cdate
	 * @param amount
	 * @return
	 */
	public static Date addHour(Date cdate, int amount) {
		Calendar cl = Calendar.getInstance();
		cl.setTime(cdate);
		cl.add(Calendar.HOUR_OF_DAY, amount);
		return cl.getTime();
	}
	
	/**
	 * 功能: 添加Date
	 * 
	 * @param cdate
	 * @param amount
	 * @return
	 */
	public static Date addDate(Date cdate, int amount) {
		Calendar cl = Calendar.getInstance();
		cl.setTime(cdate);
		cl.add(Calendar.DAY_OF_MONTH, amount);
		return cl.getTime();
	}

	/**
	 * 功能: 添加Year
	 * 
	 * @param basicDate
	 * @param year
	 * @return
	 */
	public static String addYeas(String basicDate, int year) {
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(java.sql.Date.valueOf(basicDate));
			cal.add(GregorianCalendar.YEAR, year);
			return formatDate(cal.getTime(), "yyyy-MM-dd");
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 添加Month
	 * 
	 * @param cdate
	 * @param amount
	 * @return
	 */
	public static Date addMonth(Date cdate, int amount) {
		Calendar cl = Calendar.getInstance();
		cl.setTime(cdate);
		cl.add(Calendar.MONTH, amount);
		return cl.getTime();
	}

	public static String formatDateYyyyMmDd(Date date) {
		try {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			return formatter.format(date);

		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 功能: 日期转换为字符串
	 * 
	 * @param dd
	 * @return yyyy/MM/dd
	 */
	public static String convertDateToStr(Date dd) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dd);
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		int month = calendar.get(Calendar.MONTH) + 1;
		int year = calendar.get(Calendar.YEAR);
		String testDate = "";
		testDate = testDate + year + "/";
		if (month < 10) {
			testDate = testDate + "0" + month + "/";
		} else {
			testDate += month + "/";
		}
		if (day < 10) {
			testDate = testDate + "0" + day;
		} else {
			testDate += day;
		}
		return testDate;
	}

	/**
	 * 功能:根据某个日期,返回本月的天数
	 * 
	 * @param date 本月任何一天
	 * @return 天数
	 */
	public static int getMonthsMaxDay(Date date) {
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date);
		return cal1.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 功能:获取指定日 开始本月的之后天数
	 * 
	 * @param stringDate yyyy-MM-dd
	 * @return 天数(eg:"1,2,3,")
	 */
	public static String getMonthsResidualDayNum(String stringDate) {
		Date date = formatDateYyyyMmDd(stringDate);
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date);
		String dayList = "";
		for (int i = Integer.parseInt(stringDate.substring(8)); i < cal1.getActualMaximum(Calendar.DATE) + 1; i++) {
			dayList += i + ",";
		}
		return dayList;
	}

	/**
	 * 功能:获取指定日 开始本月的之前天数
	 * 
	 * @param stringDate yyyy-MM-dd
	 * @return 天数(eg:"1,2,3,")
	 */
	public static String getMonthsAlreadyDayNum(String stringDate) {
		String dayList = "";
		for (int i = 1; i < Integer.parseInt(stringDate.substring(8)) + 1; i++) {
			dayList += i + ",";
		}
		return dayList;

	}

	/**
	 * 功能:获取指定日 开始本月的所有天数
	 * 
	 * @param stringDate yyyy-MM-dd
	 * @return 天数(eg:"1,2,3,")
	 */
	public static String getMonthsDayNum(String stringDate) {
		Date date = formatDateYyyyMmDd(stringDate);
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date);
		String dayList = "";
		for (int i = 1; i < cal1.getActualMaximum(Calendar.DATE) + 1; i++) {
			dayList += i + ",";
		}
		return dayList;

	}

	/**
	 * 功能:三个月的指定日期
	 * 
	 * @param stringDate
	 * @return
	 */
	public static Date[] getNextThreeMonths(String stringDate) {
		Date date1 = formatDateYyyyMmDd(stringDate);
		Date date2 = org.apache.commons.lang3.time.DateUtils.addMonths(date1, 1);
		Date date3 = org.apache.commons.lang3.time.DateUtils.addMonths(date2, 1);
		Date[] dataArray = { date1, date2, date3 };
		return dataArray;
	}

	/**
	 * 功能:根据某个日期,返回本月的所有日期数组
	 * 
	 * @param date 给定日期
	 * @return 本月的所有日期数组
	 */
	public static Date[] getOneMonthsDayArray(Date date) {
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date);
		cal1.set(Calendar.DATE, 1);

		int dayTimes = cal1.getActualMaximum(Calendar.DAY_OF_MONTH);
		Date[] dataArray = new Date[dayTimes];

		for (int i = 0; i < dayTimes; i++) {
			dataArray[i] = cal1.getTime();
			cal1.setTime(org.apache.commons.lang3.time.DateUtils.addDays(dataArray[i], 1));
		}
		return dataArray;
	}

	/**
	 * 功能:根据某个日期,返回monthNum个月的第一天数组
	 * 
	 * @param date 开始日期
	 * @param monthNum 月份个数
	 * @return 每个月的第一天数组
	 */
	public static Date[] getXMonthsFirstDayArray(Date date, int monthNum) {
		Date[] dataArray = new Date[monthNum];
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date);
		cal1.set(Calendar.DATE, 1);
		for (int i = 0; i < monthNum; i++) {
			dataArray[i] = org.apache.commons.lang3.time.DateUtils.addMonths(cal1.getTime(), i);
		}

		return dataArray;
	}

	/**
	 * 功能:根据1个日期,返回N天之内的日期数组
	 * 
	 * @param startDateStr 开始日期
	 * @param dayTimes 天数
	 * @return 这段时间之间的日期数组
	 */
	public static Date[] getBetweenTwoDayArray(String startDateStr, int dayTimes) {
		Date startDate = formatDateYyyyMmDd(startDateStr);
		Date[] dataArray = new Date[dayTimes];
		for (int i = 0; i < dayTimes; i++) {
			dataArray[i] = startDate;
			startDate = org.apache.commons.lang3.time.DateUtils.addDays(startDate, 1);
		}

		return dataArray;
	}

	/**
	 * 功能:根据1个日期,返回N天之内的yy-MM月份数组
	 * 
	 * @param startDateStr 开始日期
	 * @param dayTimes 天数
	 * @return 这段时间之间的日期数组
	 */
	public static String[] getBetweenTwoDayMonthArray(String startDateStr, int dayTimes) {
		Date[] dateArray = getBetweenTwoDayArray(startDateStr, dayTimes);

		String monthFirstDay = formatDateYyyyMmDd(dateArray[0]).substring(2, 7);
		List<String> dayList = new ArrayList<String>();
		dayList.add(monthFirstDay);
		for (int i = 0; i < dateArray.length; i++) {
			if (!monthFirstDay.equals(formatDateYyyyMmDd(dateArray[i]).substring(2, 7))) {

				monthFirstDay = formatDateYyyyMmDd(dateArray[i]).substring(2, 7);
				dayList.add(monthFirstDay);
			}
		}

		String[] strArray = new String[dayList.size()];
		for (int i = 0; i < strArray.length; i++) {
			strArray[i] = dayList.get(i).substring(0, 2) + dayList.get(i).substring(3, 5);
		}

		return strArray;
	}

	/**
	 * 功能:根据1个日期,返回上N个月的第一天
	 * 
	 * @param date 日期
	 * @param month 上N月
	 * @return 上N个月的第一天
	 */
	public static Date rollMonth(Date date, int month) {
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.set(Calendar.DATE, 1);
			cal.roll(Calendar.MONTH, month);

			return cal.getTime();

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * @return yyyy-MM-dd HH:mm:ss
	 */
	public static String getDateTime() {
		Date dateTime = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String strTime = format.format(dateTime);
		return strTime;
	}

	/**
	 * @return yyyy-MM-dd
	 */
	public static String getDate() {
		Date dateTime = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String strTime = format.format(dateTime);
		return strTime;
	}

	/**
	 * Get the date of monday in this week
	 * 
	 * @return yyyy-MM-dd
	 */
	public static String getMondayOfThisWeek() {
		String strTemp = "";
		Calendar c = Calendar.getInstance();
		int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (dayofweek == 0){
			dayofweek = 7;
		}

		c.add(Calendar.DATE, -dayofweek + 1);
		strTemp = c.get(1) + "-";
		if (c.get(2) + 1 < 10){
			strTemp += "0";
		}

		strTemp = strTemp + (c.get(2) + 1) + "-";
		if (c.get(5) < 10){
			strTemp += "0";
		}

		strTemp += c.get(5);
		return strTemp;
	}

	/**
	 * Get the date of sunday in this week
	 * 
	 * @return yyyy-MM-dd
	 */
	public static String getSundayOfThisWeek() {
		String strTemp = "";
		Calendar c = Calendar.getInstance();
		int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (dayofweek == 0){
			dayofweek = 7;
		}

		c.add(Calendar.DATE, -dayofweek + 7);
		strTemp = c.get(1) + "-";
		if (c.get(2) + 1 < 10){
			strTemp += "0";
		}

		strTemp = strTemp + (c.get(2) + 1) + "-";
		if (c.get(5) < 10){
			strTemp += "0";
		}

		strTemp += c.get(5);
		return strTemp;
	}

	/**
	 * Get the date of AfterDays
	 * 
	 * @param day
	 * @return yyyy-MM-dd
	 */
	public static String getTheDayAfterDays(int day) {
		String strTemp = "";
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DATE, day);
		strTemp = c.get(1) + "-";
		if (c.get(2) + 1 < 10){
			strTemp += "0";
		}

		strTemp = strTemp + (c.get(2) + 1) + "-";
		if (c.get(5) < 10){
			strTemp += "0";
		}
		strTemp += c.get(5);
		return strTemp;
	}

	/**
	 * Get data time in user's specific format
	 * 
	 * @param strFormat
	 * @return String strDtTime
	 */
	public static String getLocalDateTime(String strFormat) {
		String strDtTime = "";
		if (strFormat != null && !strFormat.equals("")) {
			SimpleDateFormat simpledateformat = new SimpleDateFormat(strFormat);
			long lngVal = System.currentTimeMillis();
			strDtTime = simpledateformat.format(new Date(lngVal));
		}
		return strDtTime;
	}

	/**
	 * Get data time in yyyyMMddHHmmss format
	 * 
	 * @return String strDtTime
	 */
	public static String getDateTimeStamp() {
		SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyyMMddHHmmss");
		long lngVal = System.currentTimeMillis();
		String strDtTime = simpledateformat.format(new Date(lngVal));
		return strDtTime;
	}

	/**
	 * Get date time in yyyyMMdd format
	 * 
	 * @return String strDtTime
	 */
	public static String getDateStamp() {
		SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyyMMdd");
		long lngVal = System.currentTimeMillis();
		String strDtTime = simpledateformat.format(new Date(lngVal));
		return strDtTime;
	}

	/**
	 * Get date time in HHmmss format
	 * 
	 * @return String strDtTime
	 */
	public static String getTimeStamp() {
		SimpleDateFormat simpledateformat = new SimpleDateFormat("HHmmss");
		long lngVal = System.currentTimeMillis();
		String strDtTime = simpledateformat.format(new Date(lngVal));
		return strDtTime;
	}

	/**
	 * Get date time in yyyy年MM月dd日hh:mm:ss format
	 * 
	 * @return String sb
	 */
	public static String getDateTimeCN() {
		StringBuffer stringbuffer = new StringBuffer();
		Calendar calendar = Calendar.getInstance();
		stringbuffer.setLength(0);
		stringbuffer.append(calendar.get(1));
		stringbuffer.append('年');
		stringbuffer.append(calendar.get(2) + 1);
		stringbuffer.append('月');
		stringbuffer.append(calendar.get(5));
		stringbuffer.append('日');
		stringbuffer.append(calendar.get(11));
		stringbuffer.append(':');
		if (calendar.get(12) < 10){
			stringbuffer.append(0);
		}

		stringbuffer.append(calendar.get(12));
		stringbuffer.append(':');
		if (calendar.get(13) < 10){
			stringbuffer.append(0);
		}

		stringbuffer.append(calendar.get(13));
		return stringbuffer.toString();
	}

	/**
	 * Get date time in Hashmap
	 * 
	 * @return a HashMap
	 */
	public static Map<String, Integer> getDateTimeToHashMap() {
		Map<String, Integer> hpDt = new HashMap<String, Integer>();
		Calendar cal = Calendar.getInstance();
		hpDt.put("YEAR", new Integer(cal.get(Calendar.YEAR)));
		hpDt.put("MONTH", new Integer(cal.get(Calendar.MONTH) + 1));
		hpDt.put("DAY", new Integer(cal.get(Calendar.DAY_OF_MONTH)));
		hpDt.put("HOUR", new Integer(cal.get(Calendar.HOUR_OF_DAY)));
		hpDt.put("MINUTE", new Integer(cal.get(Calendar.MINUTE)));
		hpDt.put("SECOND", new Integer(cal.get(Calendar.SECOND)));
		return hpDt;
	}

	/**
	 * Format time 2005-10-08 10:03:41.0 to 2005-10-08 10:03:41 .
	 * 
	 * @param str the time string
	 * @return String
	 */
	public static String getFormatTimeStamp(String str) {
		str = (str == null) ? "" : str;
		String strTemp = "";
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) == '.') {
				break;
			}
			strTemp += str.charAt(i);
		}
		return strTemp;
	}

	public static String getFormatDate(String str) {
		str = (str == null) ? "" : str;
		String strTemp = "";
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) == ' ') {
				break;
			}
			strTemp += str.charAt(i);
		}
		return strTemp;
	}

	public static boolean isInDayRange(Date availDate, Date indateFrom, Date indateTo) {
		Assert.notNull(availDate);
		Calendar c = Calendar.getInstance();
		c.setTime(availDate);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		if (indateFrom != null) {
			Calendar c1 = Calendar.getInstance();
			c1.setTime(indateFrom);
			c1.set(Calendar.HOUR_OF_DAY, 0);
			c1.set(Calendar.MINUTE, 0);
			c1.set(Calendar.SECOND, 0);
			c1.set(Calendar.MILLISECOND, 0);
			if (c.before(c1)){
				return false;
			}
		}
		if (indateTo != null) {
			Calendar c2 = Calendar.getInstance();
			c2.setTime(indateTo);
			c2.set(Calendar.HOUR_OF_DAY, 0);
			c2.set(Calendar.MINUTE, 0);
			c2.set(Calendar.SECOND, 0);
			c2.set(Calendar.MILLISECOND, 0);
			if (c.after(c2))
				return false;
		}

		return true;
	}

	/**
	 * 获取从start到end之间的日期(格式yyyyMM)
	 * 
	 * @param start
	 * @param end
	 * @return
	 */
	public static List<String> getBetweenMonths(Date start, Date end) {
		SimpleDateFormat df = new SimpleDateFormat(YEAR_MONTH);
		Date _start = org.apache.commons.lang3.time.DateUtils.truncate(start, Calendar.MONTH);
		List<String> months = new ArrayList<String>();
		while (!_start.after(end)) {
			months.add(df.format(_start));
			_start = org.apache.commons.lang3.time.DateUtils.addMonths(_start, 1);
		}
		return months;
	}
	
	/**
	 * 获取从start到end之间的日期(格式yyyyMMdd)
	 * 
	 * @param start
	 * @param end
	 * @return
	 */
	public static List<String> getBetweenDays(Date start, Date end) {
		SimpleDateFormat df = new SimpleDateFormat(SHORT_DATE);
		Date _start = org.apache.commons.lang3.time.DateUtils.truncate(start, Calendar.DAY_OF_MONTH);
		List<String> days = new ArrayList<String>();
		while (!_start.after(end)) {
			days.add(df.format(_start));
			_start = org.apache.commons.lang3.time.DateUtils.addDays(_start, 1);
		}
		return days;
	}
	
	 /**
     * 将字符串日期转换为Date
     * 
     * @param s
     * @return
     */
    public static Date convertToDate(String s) {
        DateFormat df;
        if (s == null) {
            return null;
        }
        if (s.contains(":")) {
            try {
                df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                return df.parse(s);
            } catch (Exception e) {
                log.error(e.getMessage());
                return null;
            }
        } else {
            try {
                df = new SimpleDateFormat("yyyy-MM-dd");
                return df.parse(s);
            } catch (Exception e) {
				log.error(e.getMessage());
                return null;
            }
        }
    }
    
    /**
     * 获取当期日期 
     * 
     * @return
     */
    public static Date getCurrentDate(){
    	Calendar curDate = Calendar.getInstance();
		curDate.set(Calendar.HOUR_OF_DAY, 0);
		curDate.set(Calendar.MINUTE, 0);
		curDate.set(Calendar.SECOND, 0);
		curDate.set(Calendar.MILLISECOND, 0);
		
		return curDate.getTime();
    }


	public static Date timeStampToDate(long timeStamp){
    	return new Date(timeStamp);
	}

}