Java tutorial
/* * Copyright (c) 2015 Power Group. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * * http://www.apache.org/licenses/LICENSE-2.0 * * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package org.power.commons.lang.util.time; import org.apache.commons.lang3.time.DateFormatUtils; import org.apache.commons.lang3.time.FastDateFormat; import java.text.Format; import java.text.ParseException; import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.*; /** * ?utils * * @author Kuo Hong * @version 2015-09-02 */ public class DateUtils { public static final int SECOND = 1; public static final int MINUTE = 2; public static final int HOUR = 3; public static final int DAY = 4; public static final int MILLI_FACTOR = 1000; public static final FastDateFormat ISO_DATETIME_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss"); /** * DSP ? */ public static final String DSP_DEFAULT_TIME_FORMAT = "yyyyMMddHHmmss"; private DateUtils() { } /** * #func ?23 * * @author wangchongjie * @version 1.0 */ public static Date get23HourOfDay(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); c.set(Calendar.HOUR, 23); Date date23 = c.getTime(); return date23; } /** * ? * * @param pDate Date * @param format String ? * @return String */ public static String formatDate(java.util.Date pDate, String format) { if (pDate == null) { pDate = new java.util.Date(); } SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(pDate); } /** * #func ???<br> * #desc * * @author hedan * @version 4.0 */ public static boolean isValid(String date, String pattern) { Date d = parseDate(date, pattern); return d != null && format(d, pattern).equals(date); } /** * #func ?<br> * #desc * * @author hedan * @version 4.0 */ public static String format(Date date, String pattern) { if (date == null) { return null; } return DateFormatUtils.format(date, pattern); } /** * #func ?<br> * #desc yyyy-MM-dd? * * @author hedan * @version VERSION */ public static String format(Date date) { return DateFormatUtils.format(date, DSP_DEFAULT_TIME_FORMAT); } /** * #func <br> * #desc * * @author hedan * @version 4.0 */ public static Date parseDate(String date, String pattern) { try { return org.apache.commons.lang3.time.DateUtils.parseDate(date, new String[] { pattern }); } catch (ParseException e) { return null; } } /** * #func <br> * #desc yyyy-MM-dd? * * @author hedan * @version 4.0 */ public static Date parseDate(String date) { if (date == null) { return null; } try { return org.apache.commons.lang3.time.DateUtils.parseDate(date, new String[] { DSP_DEFAULT_TIME_FORMAT }); } catch (ParseException e) { return null; } } /** * #func <br> * #desc amount? * * @author hedan * @version 4.0 */ public static Date addYears(Date date, int amount) { return org.apache.commons.lang3.time.DateUtils.addYears(date, amount); } /** * #func <br> * #desc amount? * * @author hedan * @version 4.0 */ public static Date addMonths(Date date, int amount) { return org.apache.commons.lang3.time.DateUtils.addMonths(date, amount); } /** * #func <br> * #desc amount? * * @author hedan * @version 4.0 */ public static Date addDays(Date date, int amount) { return org.apache.commons.lang3.time.DateUtils.addDays(date, amount); } /** * #func <br> * #desc amount? * * @author wangchongjie * @version 1.0 */ public static Date addMilliseconds(Date date, int amount) { return org.apache.commons.lang3.time.DateUtils.addMilliseconds(date, amount); } /** * #func <br> * #desc * * @author hedan * @version 4.0 */ public static Date addWeeks(Date date, int amount) { return org.apache.commons.lang3.time.DateUtils.addWeeks(date, amount); } /** * #func <br> * #desc ??? * * @author hedan * @version 4.0 */ public static long getTimeSpan(Date begin, Date end, int type) { long diff = end.getTime() - begin.getTime(); switch (type) { case DAY: default: return diff / org.apache.commons.lang3.time.DateUtils.MILLIS_PER_DAY; case HOUR: return diff / org.apache.commons.lang3.time.DateUtils.MILLIS_PER_HOUR; case MINUTE: return diff / org.apache.commons.lang3.time.DateUtils.MILLIS_PER_MINUTE; case SECOND: return diff / org.apache.commons.lang3.time.DateUtils.MILLIS_PER_SECOND; } } /** * #func <br> * #desc ? * * @author hedan * @version 4.0 */ public static long getDaySpan(Date begin, Date end) { return getTimeSpan(begin, end, DAY); } /** * #func ?<br> * #desc ?? * * @author hedan * @version 4.0 */ public static long getHourSpan(Date begin, Date end) { return getTimeSpan(begin, end, HOUR); } /** * #func <br> * #desc ? * * @author hedan * @version 4.0 */ public static long getMinuteSpan(Date begin, Date end) { return getTimeSpan(begin, end, MINUTE); } /** * #func <br> * #desc ? * * @author hedan * @version 4.0 */ public static long getSecondSpan(Date begin, Date end) { return getTimeSpan(begin, end, SECOND); } /** * #func ?<br> * * @author dongguoshuang */ public static int getMonthSpan(Date begin, Date end) { Calendar beginCal = new GregorianCalendar(); beginCal.setTime(begin); Calendar endCal = new GregorianCalendar(); endCal.setTime(end); int m = (endCal.get(Calendar.MONTH)) - (beginCal.get(Calendar.MONTH)); int y = (endCal.get(Calendar.YEAR)) - (beginCal.get(Calendar.YEAR)); return y * 12 + m; } /** * #func ??<br> * * @author dongguoshuang */ public static Date getFirstDayOfMonth(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int firstDay = calendar.getActualMinimum(Calendar.DAY_OF_MONTH); return org.apache.commons.lang3.time.DateUtils.setDays(date, firstDay); } /** * #func ???<br> * * @author dulin */ public static Date getLastDayOfMonth(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int firstDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); return org.apache.commons.lang3.time.DateUtils.setDays(date, firstDay); } /** * #func ??<br> * * @author dulin */ public static Date getFirstDayOfWeek(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return getADayOfWeek(calendar, Calendar.MONDAY).getTime(); } private static Calendar getADayOfWeek(Calendar day, int dayOfWeek) { int week = day.get(Calendar.DAY_OF_WEEK); if (week == dayOfWeek) { return day; } int diffDay = dayOfWeek - week; if (week == Calendar.SUNDAY) { diffDay -= 7; } else if (dayOfWeek == Calendar.SUNDAY) { diffDay += 7; } day.add(Calendar.DATE, diffDay); return day; } /** * #func ??<br> * #desc addDays? * * @author dulin * @version 4.0.14 */ public static String parseFullFormat(String dateStr, String pattern, int addDays) { try { Date date = parseDate(dateStr, pattern); if (addDays != 0) { date = addDays(date, addDays); } return DateUtils.format(date, "yyyy-MM-dd HH:mm:ss"); } catch (Exception e) { return null; } } /** * 0 * * @param date ?yyyyMMdd * @return * @throws ParseException * @author zengyunfeng * @version 1.1.0 */ public static Date strToDate(String date) throws ParseException { java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMdd"); java.util.Date d = sdf.parse(date); return d; } /** * 0 * * @param date ?yyyyMMdd * @return * @throws ParseException * @author zengyunfeng * @version 1.1.0 */ public static Date stringToDate(String date) throws ParseException { java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd"); java.util.Date d = sdf.parse(date); return d; } /** * 0 * * @param time :?yyyyMMdd:HH * @return * @throws ParseException * @author zengyunfeng * @version 1.1.0 */ public static Date strToTime(String time) throws ParseException { java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMdd:HH"); java.util.Date d = sdf.parse(time); return d; } /** * ????yyyyMMddHH * * @return String */ public static String getHourStr() { java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMddHH"); return sdf.format(new Date()); } /** * ?yyyy-MM-dd HH:mm:ss * * @param date * @return * @throws :12:32 */ public static String getDateStr(Date date) { java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(date); } /** * ?java.util.Date * * @param year int * @param month int * @param day int * @return Date java.util.Date */ public static Date getDate(int year, int month, int day) { GregorianCalendar d = new GregorianCalendar(year, month - 1, day); return d.getTime(); } public static Date getDate(int yyyyMMdd) { int dd = yyyyMMdd % 100; int yyyyMM = yyyyMMdd / 100; int mm = yyyyMM % 100; int yyyy = yyyyMM / 100; GregorianCalendar d = new GregorianCalendar(yyyy, mm - 1, dd); return d.getTime(); } /** * ?java.util.Date * * @param year int * @param month int * @param day int * @param hour int ? * @return Date java.util.Date */ public static Date getDate(int year, int month, int day, int hour) { GregorianCalendar d = new GregorianCalendar(year, month - 1, day, hour, 0); return d.getTime(); } /** * ??? ?2004-08-01 11:30:582004-08-01 11:00:00 * * @return Date java.util.Date */ public static Date getRoundedHourCurDate() { Calendar cal = GregorianCalendar.getInstance(); cal.clear(Calendar.MINUTE); cal.clear(Calendar.SECOND); cal.clear(Calendar.MILLISECOND); return cal.getTime(); } /** * ? ?2004-08-01 11:30:582004-08-01 00:00:00 * * @return Date java.util.Date */ public static Date getRoundedDayCurDate() { Calendar cal = new GregorianCalendar(); return new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)).getTime(); } /** * ?? 2004-08-01 11:30:582004-08-01 00:00:00 * * @return Date java.util.Date */ public static Date getRoundedDay(Date dt) { Calendar cal = new GregorianCalendar(); cal.setTime(dt); return new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)).getTime(); } /** * ??? 2004-08-01 11:30:582004-08-01 11:00:00 * * @param dt Date java.util.Date * @return Date java.util.Date */ public static Date getRoundedHourDate(Date dt) { Calendar cal = new GregorianCalendar(); cal.setTime(dt); cal.clear(Calendar.MINUTE); cal.clear(Calendar.SECOND); cal.clear(Calendar.MILLISECOND); return cal.getTime(); } /** * 2004-08-01 11:30:582004-08-02 * 00:00:00 2004-08-31 11:30:582004-09-01 00:00:00 * * @param dt Date java.util.Date * @return Date java.util.Date */ public static Date getNextDay(Date dt) { if (dt == null) { return dt; } Calendar cal = new GregorianCalendar(); cal.setTime(dt); // return new GregorianCalendar(cal.get(Calendar.YEAR), cal // .get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH) + 1) // // 32 ? // .getTime(); // // modified by zhuqian 2009-03-25 cal.add(Calendar.DAY_OF_YEAR, 1); return cal.getTime(); } // added by zhuqian 2009-03-25 public static Date getPreviousDay(Date dt) { if (dt == null) { return dt; } Calendar cal = Calendar.getInstance(); cal.setTime(dt); cal.add(Calendar.DAY_OF_YEAR, -1); return cal.getTime(); } /** * @param dt Date java.util.Date * @param weekDay int ?7 * @return Date java.util.Date */ public static Date getWeekDay(Date dt, int weekDay) { Calendar cal = new GregorianCalendar(); cal.setTime(dt); if (weekDay == 7) { weekDay = 1; } else { weekDay++; } cal.set(GregorianCalendar.DAY_OF_WEEK, weekDay); return cal.getTime(); } /** * N 2004-08-01 11:30:582004-08-02 * 00:00:00 2004-08-31 11:30:582004-09-01 00:00:00 * * @param dt Date java.util.Date * @return Date java.util.Date */ public static Date getNextDay(Date dt, Long n) { Calendar cal = new GregorianCalendar(); cal.setTime(dt); return new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH) + n.intValue()).getTime(); } /** * ??? 20081231 -1 20081130 * * @param dt * @param n * @return?11:16:22 */ public static Date getNextMonth(Date dt, Long n) { Calendar cal = new GregorianCalendar(); cal.setTime(dt); Calendar firstCal = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + n.intValue(), 1); if (firstCal.getActualMaximum(Calendar.DAY_OF_MONTH) < cal.get(Calendar.DAY_OF_MONTH)) { return new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + n.intValue(), firstCal.getActualMaximum(Calendar.DAY_OF_MONTH)).getTime(); } else { return new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + n.intValue(), cal.get(Calendar.DAY_OF_MONTH)).getTime(); } } /** * ??? 20081231 - 1 20081201 20080831 + 1 * 20080930 * * @param dt * @param n * @return?11:22:39 */ public static Date getNextMonthExtention(Date dt, Long n) { Calendar cal = new GregorianCalendar(); cal.setTime(dt); Calendar firstCal = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + n.intValue(), 1); if (firstCal.getActualMaximum(Calendar.DAY_OF_MONTH) < cal.get(Calendar.DAY_OF_MONTH)) { return new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + n.intValue() + 1, 1) .getTime(); } else { return new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + n.intValue(), cal.get(Calendar.DAY_OF_MONTH)).getTime(); } } public static long getBetweenDate(Date startDate, Date endDate) { long startDateTime = startDate.getTime(); long endDateTime = endDate.getTime(); long dayTime = 24 * 60 * 60 * 1000; long days = (endDateTime - startDateTime) / dayTime; return days; } public static long getMonthLength(String countDate) throws ParseException { String firstDay = countDate.substring(0, countDate.length() - 2) + "01"; Date startDate = strToDate(firstDay); Date endDate = getNextMonth(startDate, new Long(1)); long startDateTime = startDate.getTime(); long endDateTime = endDate.getTime(); long dayTime = 24 * 60 * 60 * 1000; long days = (endDateTime - startDateTime) / dayTime; return days; } /** * ? ?2004-08-01 11:30:582004-08-02 * 00:00:00 ?2004-08-31 11:30:582004-09-01 00:00:00 * * @return Date java.util.Date */ public static Date getNextDay() { Calendar cal = GregorianCalendar.getInstance(); return new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH) + 1).getTime(); } /** * java.util.Date?java.sql.Timestamp * * @param dt Date * @return Timestamp */ public static java.sql.Timestamp convertSqlDate(Date dt) { if (dt == null) { return new java.sql.Timestamp(0); } return new java.sql.Timestamp(dt.getTime()); } /** * ??"yyyyMMdd"? * * @return String */ public static String formatCurrrentDate() { java.util.Date pdate = new Date(); return format(pdate, "yyyyMMdd"); } /** * ? 2004-08-01 3:12:23 03 2004-08-01 19:12:2319 * * @param pDate Date * @return String ? */ public static String getHour(Date pDate) { return format(pDate, "HH"); } /** * ? * * @return */ public static Calendar getTheLastDayOfTheMonth(int year, int month) { Calendar cal = new GregorianCalendar(); cal.set(year, month, 1); return new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH) - 1); } public static boolean isFirstDayOfMonth(Date date) { if (date == null) { return false; } // date????? Calendar c = Calendar.getInstance(); c.setTime(date); int thisMonth = c.get(Calendar.MONTH); c.add(Calendar.DATE, -1); return thisMonth != c.get(Calendar.MONTH); } /** * ?? * * @param date * @return * @author yang_yun */ public static Date getFirstDayOfLastWeek(Date date) { Calendar c = Calendar.getInstance(); c.setTimeInMillis(date.getTime() - 604800000L); c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); Date lastWeekDate = c.getTime(); c = new GregorianCalendar(); c.setFirstDayOfWeek(Calendar.MONDAY); c.setTime(new Date(lastWeekDate.getTime())); c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday return c.getTime(); } /** * ????YYYYMMDD??? * * @param dateString ? * @return true?false * @author zhangpeng mrd3.4.0 */ public static boolean validateDateString(String dateString) { if (dateString == null || dateString.equals("")) { return false; } // ?YYYYMMDD?,2000 String regDate = "^(((([02468][048])|([13579][26]))[0]{2})(02)(([0][1-9])|([1-2][0-9])))" + // ?2100 "|(((([02468][1235679])|([13579][01345789]))[0]{2})(02)(([0][1-9])|([1][0-9])|([2][0-8])" + "))" + // ?1996 "|(([0-9]{2}(([0][48])|([2468][048])|([13579][26])))(02)(([0][1-9])|([1-2][0-9])))" + // ??1997 "|(([0-9]{2}(([02468][1235679])|([13579][01345789])))(02)(([0][1-9])|([1][0-9])|" + "([2][0-8])))" + // 31 "|(([0-9]{4})(([0]{1}(1|3|5|7|8))|10|12)(([0][1-9])|([1-2][0-9])|30|31))" + // ??30 "|(([0-9]{4})(([0]{1}(4|6|9))|11)(([0][1-9])|([1-2][0-9])|30))$"; return dateString.matches(regDate); } /** * ?0 2008-11-13 16:002008-11-13 00:00 */ public static Calendar getDateCeil(Date date) { Calendar cal = new GregorianCalendar(); cal.setTime(date); return new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)); } /** * ?23 2008-11-13 16:002008-11-13 23:00 */ public static Calendar getDateFloor(Date date) { Calendar cal = new GregorianCalendar(); cal.setTime(date); return new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 23, 0); } /** * ?0 */ public static Calendar getCurDateCeil() { Calendar cal = new GregorianCalendar(); return new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)); } /** * ?23 */ public static Calendar getCurDateFloor() { Calendar cal = new GregorianCalendar(); return new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 23, 0); } public static Date getStartOfDate(Date date) { if (date == null) { return date; } Calendar c = Calendar.getInstance(); c.setTimeInMillis(date.getTime()); c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); return c.getTime(); } public static Date getEndOfDate(Date date) { if (date == null) { return date; } Calendar c = Calendar.getInstance(); c.setTimeInMillis(date.getTime()); c.set(Calendar.HOUR_OF_DAY, 23); c.set(Calendar.MINUTE, 59); c.set(Calendar.SECOND, 59); return c.getTime(); } /** * @return long * @throws * @Title: getTimeInSeconds * @Description: ? */ public static long getTimeInSeconds() { Calendar c = Calendar.getInstance(); return c.getTimeInMillis() / MILLI_FACTOR; } /** * ?? * * @param * @return ? * @author zhangpingan * @author zhangpingan mrd Beidou2.0.0 */ public static long getTimeInMillionSeconds() { Calendar c = Calendar.getInstance(); return c.getTimeInMillis(); } /** * @param dateString * @param format1 yyyyMMdd * @param format2 , yyyy/MM/dd * @return * @author zhangpingan */ public static String formatDateString(String dateString, String format1, String format2) { if (dateString == null) { return null; } java.text.SimpleDateFormat beforeFormat = new java.text.SimpleDateFormat(format1); java.text.SimpleDateFormat endFormat = new java.text.SimpleDateFormat(format2); try { return endFormat.format(beforeFormat.parse(dateString)); } catch (ParseException e) { e.printStackTrace(); return dateString; } } /** * ?doris? * * @param doirsDate * @return * @author kanghongwei */ public static String formatDorisReturnDate(Object doirsDate, Format format) { if (doirsDate == null || (!(doirsDate instanceof Date))) { return null; } Date date = (Date) doirsDate; Format localFormat = (SimpleDateFormat) format; return localFormat.format(date); } /** * ? * * @param startDate * @param endDate * @param format * @return * @author kanghongwei */ public static List<String> getDayListBetween2Day(String startDate, String endDate, Format format) { List<String> dayList = new ArrayList<String>(); int dataNum = Integer.parseInt(getTwoDay2String(startDate, endDate, format)); for (int i = 0; i < (dataNum + 1); i++) { String resultDate = getNextDay2String(startDate, String.valueOf(i), format); dayList.add(resultDate); } return dayList; } /** * fotmat, * * @param startDate * @param endDate * @param format * @return * @author kanghongwei */ public static String getTwoDay2String(String startDate, String endDate, Format format) { SimpleDateFormat localFormat = (SimpleDateFormat) format; long day = 0; try { java.util.Date startDay = localFormat.parse(startDate); java.util.Date endDay = localFormat.parse(endDate); day = (endDay.getTime() - startDay.getTime()) / (24 * 60 * 60 * 1000); } catch (Exception e) { return ""; } return day + ""; } /** * fotmat, * * @param startDate * @param endDate * @param format * @return * @author zhangbi */ public static long getTwoDaysBetweenNumber(String startDate, String endDate, Format format) { SimpleDateFormat localFormat = (SimpleDateFormat) format; long day = 0; try { java.util.Date startDay = localFormat.parse(startDate); java.util.Date endDay = localFormat.parse(endDate); day = (endDay.getTime() - startDay.getTime()) / (24 * 60 * 60 * 1000); } catch (Exception e) { return 0; } return day; } /** * ????(?delay) * * @param nowdate * @param delay * @param format * @return * @author kanghongwei */ public static String getNextDay2String(String nowdate, String delay, Format format) { try { SimpleDateFormat localFormat = (SimpleDateFormat) format; String mdate = ""; Date date = strToDateWithFormat(nowdate, localFormat); long myTime = (date.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60; date.setTime(myTime * 1000); mdate = localFormat.format(date); return mdate; } catch (Exception e) { return ""; } } /** * fotmat, Stringutil.Date * * @param strDate * @param format * @return * @author kanghongwei */ public static Date strToDateWithFormat(String strDate, Format format) { SimpleDateFormat localFormat = (SimpleDateFormat) format; ParsePosition pos = new ParsePosition(0); Date strtodate = localFormat.parse(strDate, pos); return strtodate; } /** * specificDate?startDateendDate * * @param startDate * @param endDate * @param specificDate * @return * @author kanghongwei */ public static boolean isBetweenDate(Date startDate, Date endDate, Date specificDate) { long startTm = getDateCeil(startDate).getTimeInMillis(); long endTm = getDateCeil(endDate).getTimeInMillis(); long specificTm = getDateCeil(specificDate).getTimeInMillis(); if (startTm <= specificTm && endTm >= specificTm) { return true; } return false; } /** * specificDate?startDateendDate * * @param startDate * @param endDate * @param format * @param specificDate * @return * @author kanghongwei */ public static boolean isBetweenDate(String startDate, String endDate, Format format, String specificDate) { boolean result = false; List<String> dateList = getDayListBetween2Day(startDate, endDate, format); for (String date : dateList) { if (date.equalsIgnoreCase(specificDate)) { return true; } } return result; } public static void main(String args[]) { System.out.println(ISO_DATETIME_FORMAT.format(new Date())); Date d = null; System.out.println(ISO_DATETIME_FORMAT.format(d)); Date t = addMonths(new Date(), 13); System.out.println(t.toString()); t = addDays(t, 20); System.out.println(t.toString()); } /** * ??? * * @param format * @param date */ public static String getDateString(String format, Date date) { SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(date); } /** * ??? * * @param date1Start 1 * @param date1End 1? * @param date2Start 2 * @param date2End 2? * @return <p> * <code>true</code>:?? * <p> * <code>false</code>:?? */ public static boolean isOverlay(Date date1Start, Date date1End, Date date2Start, Date date2End) { return ((date1Start.getTime() >= date2Start.getTime()) && date1Start.getTime() <= date2End.getTime()) || ((date2Start.getTime() >= date1Start.getTime()) && date2Start.getTime() <= date1End.getTime()); } /** * ??? * * @param date1StartStr 1 * @param date1EndStr 1? * @param date2StartStr 2 * @param date2EndStr 2? * @param patten * @return <p> * <code>true</code>:?? * <p> * <code>false</code>:?? */ public static boolean isOverlay(String date1StartStr, String date1EndStr, String date2StartStr, String date2EndStr, String patten) { Date date1Start = DateUtils.parseDate(date1StartStr, patten); Date date1End = DateUtils.parseDate(date1EndStr, patten); Date date2Start = DateUtils.parseDate(date2StartStr, patten); Date date2End = DateUtils.parseDate(date2EndStr, patten); return isOverlay(date1Start, date1End, date2Start, date2End); } /** * ? * * @param startDate * @param endDate ? */ public static long getDaysBetweenIgnoreTime(String startDate, String endDate, Format format) { SimpleDateFormat localFormat = (SimpleDateFormat) format; try { java.util.Date startDay = localFormat.parse(startDate); java.util.Date endDay = localFormat.parse(endDate); return getDaysBetweenIgnoreTime(startDay, endDay); } catch (Exception e) { return 0; } } /** * ? * * @param startDate * @param endDate ? */ public static long getDaysBetweenIgnoreTime(Date startDate, Date endDate) { startDate = getRoundedDay(startDate); endDate = getNextDay(endDate); return getBetweenDate(startDate, endDate); } }