|
|
@@ -15,11 +15,40 @@ import java.util.GregorianCalendar;
|
|
|
*/
|
|
|
public class DateUtils {
|
|
|
|
|
|
+ /**
|
|
|
+ * 时间比较,默认
|
|
|
+ */
|
|
|
public static final int COMPARE_NONE = 0;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 时间比较,精确到秒
|
|
|
+ */
|
|
|
public static final int COMPARE_SECOND = 1;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 时间比较,精确到分钟
|
|
|
+ */
|
|
|
public static final int COMPARE_MINUTE = 2;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 时间比较,精确到小时
|
|
|
+ */
|
|
|
public static final int COMPARE_HOUR = 3;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 时间比较,精确到天
|
|
|
+ */
|
|
|
public static final int COMPARE_DAY = 4;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 时间比较,精确到月份
|
|
|
+ */
|
|
|
+ public static final int COMPARE_MONTH = 5;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 时间比较,精确到年份
|
|
|
+ */
|
|
|
+ public static final int COMPARE_YEAR = 6;
|
|
|
static final SimpleDateFormat ym = new SimpleDateFormat("yyyyMM");
|
|
|
static final SimpleDateFormat YM = new SimpleDateFormat("yyyy-MM");
|
|
|
public static final SimpleDateFormat YMD = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
@@ -27,6 +56,9 @@ public class DateUtils {
|
|
|
static final FastDateFormat MM = FastDateFormat.getInstance("MM");
|
|
|
static final FastDateFormat YYYY = FastDateFormat.getInstance("yyyy");
|
|
|
static final FastDateFormat DD = FastDateFormat.getInstance("dd");
|
|
|
+ static final SimpleDateFormat COMPARE_HOUR_PATTERN = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
|
|
|
+ static final SimpleDateFormat COMPARE_MINUTE_PATTERN = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
|
|
|
+ static final SimpleDateFormat COMPARE_SECOND_PATTERN = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
|
/**
|
|
|
* 获取月份
|
|
|
@@ -269,4 +301,46 @@ public class DateUtils {
|
|
|
calendar.add(Calendar.MONTH, increase);
|
|
|
return calendar.getTime();
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 比较日期大小
|
|
|
+ *
|
|
|
+ * @param firstDate 时间1
|
|
|
+ * @param secondDate 时间2
|
|
|
+ * @param compareType 比较类型
|
|
|
+ * 精确位(天、小时、分钟、秒)
|
|
|
+ * @return 1:firstDate > secondDate; 0: firstDate = secondDate; -1: firstDate < secondDate;
|
|
|
+ */
|
|
|
+ public static int dateCompare(Date firstDate, Date secondDate, int compareType) {
|
|
|
+ String firstDateParam;
|
|
|
+ String secondDateParam;
|
|
|
+ if (COMPARE_NONE == compareType) {
|
|
|
+ return firstDate.compareTo(secondDate);
|
|
|
+ } else if (COMPARE_SECOND == compareType) {
|
|
|
+ firstDateParam = COMPARE_SECOND_PATTERN.format(firstDate);
|
|
|
+ secondDateParam = COMPARE_SECOND_PATTERN.format(secondDate);
|
|
|
+ return firstDateParam.compareTo(secondDateParam);
|
|
|
+ } else if (COMPARE_MINUTE == compareType) {
|
|
|
+ firstDateParam = COMPARE_MINUTE_PATTERN.format(firstDate);
|
|
|
+ secondDateParam = COMPARE_MINUTE_PATTERN.format(secondDate);
|
|
|
+ return firstDateParam.compareTo(secondDateParam);
|
|
|
+ } else if (COMPARE_HOUR == compareType) {
|
|
|
+ firstDateParam = COMPARE_HOUR_PATTERN.format(firstDate);
|
|
|
+ secondDateParam = COMPARE_HOUR_PATTERN.format(secondDate);
|
|
|
+ return firstDateParam.compareTo(secondDateParam);
|
|
|
+ } else if (COMPARE_DAY == compareType) {
|
|
|
+ firstDateParam = YMD.format(firstDate);
|
|
|
+ secondDateParam = YMD.format(secondDate);
|
|
|
+ return firstDateParam.compareTo(secondDateParam);
|
|
|
+ } else if (COMPARE_MONTH == compareType) {
|
|
|
+ firstDateParam = YM.format(firstDate);
|
|
|
+ secondDateParam = YM.format(secondDate);
|
|
|
+ return firstDateParam.compareTo(secondDateParam);
|
|
|
+ } else if (COMPARE_YEAR == compareType) {
|
|
|
+ firstDateParam = YYYY.format(firstDate);
|
|
|
+ secondDateParam = YYYY.format(secondDate);
|
|
|
+ return firstDateParam.compareTo(secondDateParam);
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
}
|