JAVA常用日期比较操作说明

来源:互联网 发布:北京软件开发平均工资 编辑:程序博客网 时间:2024/06/01 08:13

转载地址:点击打开链接  http://xiaowei-qi-epro-com-cn.iteye.com/blog/1682055

[java] view plain copy
  1. import java.text.DateFormat;   
  2. import java.text.ParseException;   
  3. import java.text.SimpleDateFormat;   
  4. import java.util.Calendar;   
  5. import java.util.Date;   
  6. public class DateUtils {   
  7. /**  
  8. * 一个日期早于一个日期区间  
  9. */   
  10. public static final int BEFORE_START_DATE = -2;   
  11.   
  12. /**  
  13. * 一个日期等于一个日期区间的开始日期  
  14. */   
  15. public static final int EQUAL_START_DATE = -1;   
  16.   
  17. /**  
  18. * 一个日期在一个日期区间之内  
  19. */   
  20. public static final int BETWEEN_TOW_DATE = 0;   
  21.   
  22. /**  
  23. * 一个日期等于一个日期区间的结束日期  
  24. */   
  25. public static final int EQUAL_END_DATE = 1;   
  26.   
  27. /**  
  28. * 一个日期晚于一个日期区间  
  29. */   
  30. public static final int AFTER_END_DATE = 2;   
  31.   
  32. /**  
  33. * 三个日期相等  
  34. */   
  35. public static final int TREE_DATE_EQUAL = 3;   
  36.   
  37. /**  
  38. * 普通日期格式  
  39. */   
  40. public static final String NORMAL_DATE_PATTERN = "yyyy-MM-dd";   
  41.   
  42. public static final String ZHCN_DATE_PATTERN = "yyyy年MM月dd日";   
  43.   
  44. public static final String ZHCN_DATE_TIME_PATTERN = "yyyy年MM月dd日 HH:mm:ss";   
  45.   
  46. /**值班日期(白班)每天的起始时间点,或(晚班)的结束时间点*/   
  47. public static String dutyStartData=PropertiesBean.getProperty("DUTY_START_DATA","9:00:00");   
  48.   
  49. /**值班日期(白班)每天的结束时间点,或(晚班)的起始时间点*/   
  50. public static String dutyEndData=PropertiesBean.getProperty("DUTY_END_DATA","18:30:00");   
  51.   
  52. private DateUtils() {   
  53. }   
  54.   
  55. /**  
  56.  
  57. * @Description:判断<firstDate>时间点是否在<secondDate>时间点之前  
  58. * 如果此 firstDate 的时间在参数<secondDate>表示的时间之前,则返回小于 0 的值  
  59. * @param firstDate  
  60. * @param secondDate  
  61. * @return  
  62. * @ReturnType boolean  
  63. * @author:  
  64. * @Created 2012 2012-9-20上午08:40:33  
  65. */   
  66. public static boolean isBefore(Date firstDate, Date secondDate) {   
  67.   
  68. return compare(firstDate, secondDate) < 0 ? true : false;   
  69. }   
  70.   
  71.    /**  
  72.     *  
  73.     * @Description:判断<firstDate>时间点是否在<secondDate>时间点之后  
  74.     * 如果此 firstDate 的时间在参数<secondDate>表示的时间之后,则返回大于 0 的值  
  75.     * @param firstDate  
  76.     * @param secondDate   
  77.     * @ReturnType boolean  
  78.     * @author:  
  79.     * @Created 2012 2012-9-20上午08:38:48  
  80.     */   
  81. public static boolean isAfter(Date firstDate, Date secondDate) {   
  82.   
  83. return compare(firstDate, secondDate) > 0 ? true : false;   
  84. }   
  85.   
  86. /**  
  87.  
  88. * @Description:比较两个时间点是否相等  
  89. * @param firstDate  
  90. * @param secondDate  
  91. * @ReturnType boolean  
  92. * @author:  
  93. * @Created 2012 2012-9-20上午08:37:30  
  94. */   
  95. public static boolean isEqual(Date firstDate, Date secondDate) {   
  96.   
  97. return compare(firstDate, secondDate) == 0 ? true : false;   
  98. }   
  99.   
  100. /**  
  101.  
  102. * @Description:比较两个时间点  
  103. * 如果secondDate表示的时间等于此 firstDate 表示的时间,则返回 0 值;  
  104. * 如果此 firstDate 的时间在参数<secondDate>表示的时间之前,则返回小于 0 的值;  
  105. * 如果此 firstDate 的时间在参数<secondDate>表示的时间之后,则返回大于 0 的值  
  106. * @param firstDate  
  107. * @param secondDate  
  108. * @ReturnType int  
  109. * @author:  
  110. * @Created 2012 2012-9-20上午08:34:33  
  111. */   
  112. public static int compare(Date firstDate, Date secondDate) {   
  113.   
  114. Calendar firstCalendar = null;   
  115. /**使用给定的 Date 设置此 Calendar 的时间。**/   
  116. if (firstDate != null) {   
  117. firstCalendar = Calendar.getInstance();   
  118. firstCalendar.setTime(firstDate);   
  119. }   
  120.   
  121. Calendar secondCalendar = null;   
  122. /**使用给定的 Date 设置此 Calendar 的时间。**/   
  123. if (firstDate != null) {   
  124. secondCalendar = Calendar.getInstance();   
  125. secondCalendar.setTime(secondDate);   
  126. }   
  127.   
  128. try {   
  129. /**  
  130. * 比较两个 Calendar 对象表示的时间值(从历元至现在的毫秒偏移量)。  
  131. * 如果参数表示的时间等于此 Calendar 表示的时间,则返回 0 值;  
  132. * 如果此 Calendar 的时间在参数表示的时间之前,则返回小于 0 的值;  
  133. * 如果此 Calendar 的时间在参数表示的时间之后,则返回大于 0 的值  
  134. * **/   
  135. return firstCalendar.compareTo(secondCalendar);   
  136. catch (NullPointerException e) {   
  137. throw new IllegalArgumentException(e);   
  138. catch (IllegalArgumentException e) {   
  139. throw new IllegalArgumentException(e);   
  140. }   
  141. }   
  142.   
  143. /**  
  144.  
  145. * @Description:  
  146. * @param startDate  
  147. * @param endDate  
  148. * @param inDate  
  149. * @return  
  150. * @ReturnType int  
  151. * @author:  
  152. * @Created 2012 2012-9-20上午08:42:06  
  153. */   
  154. public static int betweenTowDate(Date startDate, Date endDate, Date inDate) {   
  155.   
  156. /**  
  157. * 判断<endDate>时间点是否在<startDate>时间点之前  
  158. * 如果为真表示<endDate>时间点在<startDate>时间点之前则抛出异常  
  159. * 即结束时间在开始时间之前是不正常的  
  160. */   
  161. if (isBefore(endDate, startDate)) {   
  162. throw new IllegalArgumentException(   
  163. "endDate can not before startDate!");   
  164. }   
  165.   
  166. /**  
  167. * 比较两个时间点<inDate>和<startDate>  
  168.      * 如果inDate表示的时间等于此 startDate 表示的时间,则返回 0 值;  
  169.      * 如果此 inDate 的时间在参数<startDate>表示的时间之前,则返回小于 0 的值;  
  170.      * 如果此 inDate 的时间在参数<startDate>表示的时间之后,则返回大于 0 的值  
  171. */   
  172. int sflag = compare(inDate,startDate);   
  173.   
  174. /**  
  175. * 比较两个时间点<inDate>和<endDate>  
  176.      * 如果inDate表示的时间等于此 endDate 表示的时间,则返回 0 值;  
  177.      * 如果此 inDate 的时间在参数<endDate>表示的时间之前,则返回小于 0 的值;  
  178.      * 如果此 inDate 的时间在参数<endDate>表示的时间之后,则返回大于 0 的值  
  179. */   
  180. int eflag = compare(inDate,endDate);   
  181.   
  182. int flag = 0;   
  183.           
  184. /**如果此 inDate 的时间在参数<startDate>表示的时间之前,则返回小于 0 的值**/   
  185. if (sflag < 0){   
  186. /**一个日期早于一个日期区间**/   
  187. flag = DateUtils.BEFORE_START_DATE;   
  188. }else if (sflag == 0) { /**如果inDate表示的时间等于此 startDate 表示的时间,则返回 0 值;**/   
  189. /**如果inDate表示的时间等于此 endDate 表示的时间,则返回 0 值;**/   
  190. if (eflag == 0) {   
  191. /**三个日期相等**/   
  192. flag = DateUtils.TREE_DATE_EQUAL;   
  193. }else {   
  194. /**一个日期等于一个日期区间的开始日期**/   
  195. flag = DateUtils.EQUAL_START_DATE;   
  196. }   
  197. }else if (sflag > 0 && eflag < 0) {/**满足-inDate 的时间在参数<startDate>表示的时间之后,和 inDate 的时间在参数<endDate>表示的时间之前**/   
  198. /**一个日期在一个日期区间之内**/   
  199. flag = DateUtils.BETWEEN_TOW_DATE;   
  200. else if (eflag == 0) {/**如果inDate表示的时间等于此 endDate 表示的时间**/   
  201. /**一个日期等于一个日期区间的结束日期**/   
  202. flag = DateUtils.EQUAL_END_DATE;   
  203. else if (eflag > 0) {/**满足inDate 的时间在参数<endDate>表示的时间之后**/   
  204. /**一个日期晚于一个日期区间**/   
  205. flag = DateUtils.AFTER_END_DATE;   
  206. }   
  207. return flag;   
  208. }   
  209.   
  210. /**  
  211.  
  212. * @Description:分别判断当前日期是与一个日期区间的六种情况比较  
  213. * (1) 一个日期早于一个日期区间  
  214. * (2)三个日期相等  
  215. * (3)一个日期等于一个日期区间的开始日期  
  216. * (4)一个日期在一个日期区间之内  
  217. * (5)一个日期等于一个日期区间的结束日期  
  218. * (6)一个日期晚于一个日期区间  
  219. * @param startDate  
  220. * @param endDate  
  221. * @return  
  222. * @ReturnType int  
  223. * @author:  
  224. * @Created 2012 2012-9-20上午09:00:31  
  225. */   
  226. public static int betweenTowDate(Date startDate, Date endDate) {   
  227. return betweenTowDate(startDate, endDate, new Date());   
  228. }   
  229.   
  230.   
  231. /**  
  232. * 将Date型转换为String型  
  233.  
  234. * @param dtDate  
  235. *            Date 要转换的时间  
  236. * @param strPattern  
  237. *            String 转换的格式  
  238. * @return String 转换后的时间 说明:格式可以为 yyyy-MM-dd HH:mm:ss等,可任意组合,如 yyyy年MM月dd日  
  239. *         yyyy代表年 MM代表月 dd代表日 HH hh kk代表小时 mm代表分钟 ss代表秒钟 format  
  240. */   
  241. public static String dateToString(Date dtDate, String strPattern) {   
  242. SimpleDateFormat lsdfDate = new SimpleDateFormat(strPattern);   
  243. return lsdfDate.format(dtDate);   
  244. }   
  245.   
  246.   
  247. /**  
  248.  
  249. * @Description:将符合相应格式的字符串转化为日期 <格式自定义>  
  250. * @param dateStr 日期字符串  
  251. * @param pattern 日期格式  
  252. * @ReturnType Date 日期字串为空或者不符合日期格式时返回null  
  253. * @author:  
  254. * @Created 2012 2012-9-20上午09:06:00  
  255. */   
  256. public static Date getDate(String dateStr, String pattern) {   
  257.   
  258. return getDate(dateStr, pattern, null);   
  259. }   
  260.   
  261.   
  262. /**  
  263. * 将符合相应格式的字符串转化为日期 格式自定义  
  264.  
  265. * @param dateStr  
  266. *            日期字符串  
  267. * @param pattern  
  268. *            日期格式  
  269. * @param defaultDate  
  270. *            默认日期  
  271. * @return 日期字串为空或者不符合日期格式时返回defaultDate  
  272. */   
  273. public static Date getDate(String dateStr, String pattern, Date defaultDate) {   
  274.   
  275. if (dateStr != null && pattern != null) {   
  276. SimpleDateFormat sdf = new SimpleDateFormat(pattern);   
  277. try {   
  278. return sdf.parse(dateStr);   
  279. catch (ParseException e) {   
  280.   
  281. }   
  282. }   
  283. return defaultDate;   
  284. }   
  285.   
  286.   
  287.   
  288. /**  
  289.  
  290. * @Description:将某种日期转换成指定格式的日期数据 <获取相应格式的Date>  
  291. * @param date          需要格式的日期参数  
  292. * @param pattern       日期格式  
  293. * @ReturnType Date  
  294. * @author:  
  295. * @Created 2012 2012-9-20上午09:08:24  
  296. */   
  297. public static Date getFormatDate(Date date, String pattern) {   
  298.   
  299. if (date == null) {   
  300. throw new IllegalArgumentException("date can not be null!");   
  301. }   
  302.   
  303. if (pattern == null) {   
  304. pattern = NORMAL_DATE_PATTERN;   
  305. }   
  306.   
  307. SimpleDateFormat sdf = new SimpleDateFormat(pattern);   
  308.   
  309. String dateStr = sdf.format(date);   
  310. try {   
  311. return sdf.parse(dateStr);   
  312. catch (ParseException e) {   
  313.   
  314. }   
  315. return date;   
  316. }   
  317.   
  318.   
  319. /**  
  320.  
  321. * @Description:将符合相应格式的字符串转化为日期 格式 yyyy-MM-dd  
  322. * @param dateStr  
  323. * @return 日期字串为空或者不符合日期格式时返回null  
  324. * @ReturnType Date  
  325. * @author:  
  326. * @Created 2012 2012-9-20上午09:04:28  
  327. */   
  328. public static Date getDate(String dateStr) {   
  329. return getDate(dateStr, DateUtils.NORMAL_DATE_PATTERN);   
  330. }   
  331.   
  332.   
  333. /**  
  334.  
  335. * @Description:将当前日期转换成字符串 格式 yyyy-MM-dd  
  336. * @ReturnType String  
  337. * @author:  
  338. * @Created 2012 2012-9-19下午05:54:42  
  339. */   
  340. public static String datetoStr() {   
  341. SimpleDateFormat sdf = new SimpleDateFormat(NORMAL_DATE_PATTERN);   
  342. String curDate = sdf.format(new Date());   
  343. return curDate;   
  344. }   
  345.   
  346.   
  347. /**  
  348.  
  349. * @Description: 获取当天日期,日期格式为YYYYMMDD  
  350. * @ReturnType String  
  351. * @author:  
  352. * @Created 2012 2012-9-20上午09:17:19  
  353. */   
  354. public static String getIntraday() {   
  355. Calendar date = Calendar.getInstance();// 获得当前日期   
  356. int year = date.get(Calendar.YEAR);   
  357. int month = date.get(Calendar.MONTH) + 1;   
  358. int day = date.get(Calendar.DAY_OF_MONTH);   
  359. String updateFileDate = new String(new Integer(year).toString()   
  360. new Integer(month).toString() + new Integer(day).toString());   
  361. return updateFileDate;   
  362. }   
  363.   
  364. /**  
  365.  
  366. * @Description:获取当天日期,日期格式为YYYY-MM-DD HH:mm:ss  
  367. * @return  
  368. * @ReturnType Date  
  369. * @author:  
  370. * @Created 2012 2012-9-20上午09:58:36  
  371. */   
  372. public static Date getCurrentlyDate(){   
  373. Date currentDate=new Date();   
  374. return getFormatDate(currentDate,"yyyy-MM-dd HH:mm:ss");   
  375. }   
  376.   
  377.   
  378. /**  
  379.  
  380. * @Description: 获取时间的小时数(24小时制)  
  381. * @param date  
  382. * @return  
  383. * @ReturnType int  
  384. * @author:  
  385. * @Created 2012 2012-9-25下午08:33:44  
  386. */   
  387. public static int getTime24Hour() {   
  388.   
  389. Calendar calender = Calendar.getInstance();   
  390. calender.setTime(getCurrentlyDate());   
  391. return calender.get(Calendar.HOUR_OF_DAY);   
  392. }   
  393.   
  394. /**  
  395.  
  396. * @Description:获取前一日  
  397. * @param date  
  398. * @return  
  399. * @ReturnType Date  
  400. * @author:  
  401. * @Created 2012 2012-9-25下午08:32:00  
  402. */   
  403. public static Date getBeforeDate(Date date) {   
  404. Calendar calender = Calendar.getInstance();   
  405. calender.setTime(date);   
  406. calender.add(calender.DATE, -1);   
  407. return calender.getTime();   
  408.     }   
  409.   
  410. /**  
  411.  
  412. * @Description:取得当前日期的下一日  
  413. * @ReturnType String  
  414. * @author:  
  415. * @Created 2012 2012-9-20上午09:12:06  
  416. */   
  417. public static String getTomorrowDate(){   
  418. Date myDate=new Date();     
  419. Calendar calender = Calendar.getInstance();     
  420. calender.setTime(myDate);     
  421. calender.add(calender.DATE,1);     
  422. return dateToString(calender.getTime(),"yyyy-MM-dd");    
  423. }   
  424.   
  425. /**  
  426.  
  427. * @Description:取当前日期后的第二日  
  428. * @return  
  429. * @ReturnType String  
  430. * @author:  
  431. * @Created 2012 2012-9-20上午10:37:47  
  432. */   
  433.     public static String getNextTomorrowDate(){   
  434.   
  435. DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");   
  436.   
  437. Date myDate=new Date();   
  438.   
  439. long myTime=(myDate.getTime()/1000)-60*60*24*365;   
  440.   
  441. myDate.setTime(myTime*1000);   
  442.   
  443. String mDate=formatter.format(myDate);   
  444.   
  445. myTime=(myDate.getTime()/1000)+60*60*24;   
  446.   
  447. myDate.setTime(myTime*1000);   
  448.   
  449. mDate=formatter.format(myDate);   
  450.   
  451. return mDate;   
  452. }   
  453.       
  454.       
  455.     /**  
  456.      *  
  457.      * @Description:获取当前星期在当前月份中的第几个星期  
  458.      * @param date  
  459.      * @return  
  460.      * @ReturnType int  
  461.      * @author:  
  462.      * @Created 2012 2012-10-29上午11:45:13  
  463.      */   
  464.     public static int getTimeWeekOfMonth(Date date) {       
  465.     Calendar calender = Calendar.getInstance();   
  466.     calender.setTime(date);   
  467.     return calender.get(Calendar.WEEK_OF_MONTH);   
  468.     }   
  469.       
  470.       
  471.     /**  
  472.     *  
  473.     * 功能描述:获取时间在当前星期的第几天  
  474.     *  
  475.     * @author    
  476.     * <p>创建日期 :2012 2012-10-29上午11:45:52</p>  
  477.     *  
  478.     * @param date  
  479.     * @return 返回星期数,其中: 1表示星期一, ...7表示星期天  
  480.     *  
  481.     * <p>修改历史 :(修改人,修改时间,修改原因/内容)</p>  
  482.     */   
  483.     public  static int getTimeDateOfWeek(Date date) {      
  484.     Calendar calender = Calendar.getInstance();   
  485.     calender.setTime(date);   
  486.     int week = calender.get(Calendar.DAY_OF_WEEK) - 1;   
  487.     if(week == 0)   
  488.     week = 7;   
  489.     return week;   
  490.     }   
  491.       
  492.       
  493.       
  494.     /**  
  495.      *  
  496.      * 功能描述:获取时间在当前星期的第几天  
  497.      *  
  498.      * @author    
  499.      * <p>创建日期 :2012 2012-10-29上午11:45:52</p>  
  500.      *  
  501.      * @param date  
  502.      * @return 返回星期数,其中: 7表示星期六..1表示星期天  
  503.      *  
  504.      * <p>修改历史 :(修改人,修改时间,修改原因/内容)</p>  
  505.      */   
  506.      public  static int getQuartzTimeDateOfWeek(Date date) {      
  507.      Calendar calender = Calendar.getInstance();   
  508.      calender.setTime(date);   
  509.      int week = calender.get(Calendar.DAY_OF_WEEK);       
  510.      return week;   
  511.      }   
  512.       
  513.      /**  
  514.       *  
  515.       * @Description:某个时间与当前时间进行求差  
  516.       * @param date  
  517.       * @return  
  518.       * @ReturnType long  
  519.       * @author:  
  520.       * @Created 2012 2012-12-12下午01:00:53  
  521.       */   
  522.      public static long getAppointTimeDifference(Date startDate,Date endDate){     
  523.           long l=endDate.getTime()-startDate.getTime();             
  524.           long day=l/(24*60*60*1000);   
  525.   return day;   
  526.      }   
  527.        
  528.        
  529.      /**  
  530.       *  
  531.       * @Description:某个时间与当前时间进行求差  
  532.       * @param date  
  533.       * @return  
  534.       * @ReturnType long  
  535.       * @author:  
  536.       * @Created 2012 2012-12-12下午01:00:53  
  537.       */   
  538.      public static long getTimeDifference(Date date){      
  539.       /**获取当前系统时间**/   
  540.   java.util.Date currentlyDate=DateUtils.getCurrentlyDate();   
  541.     
  542.           long l=date.getTime()-currentlyDate.getTime();     
  543.           
  544.           long day=l/(24*60*60*1000);   
  545.             
  546.           long hour=(l/(60*60*1000)-day*24);     
  547.             
  548.           long min=((l/(60*1000))-day*24*60-hour*60);     
  549.             
  550.           long s=(l/1000-day*24*60*60-hour*60*60-min*60);   
  551.              
  552.           System.out.println(""+day+"天"+hour+"小时"+min+"分"+s+"秒");            
  553. return day;   
  554.      }   
  555.        
  556.      public static void main(String[] args) {           
  557.     SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");     
  558.     java.util.Date now;   
  559.   try {   
  560.   now = df.parse("2012-12-10 13:31:34");   
  561.   
  562.   java.util.Date date=DateUtils.getCurrentlyDate();   
  563.   
  564.           long l=now.getTime()-date.getTime();     
  565.           
  566.           long day=l/(24*60*60*1000);     
  567.             
  568.           long hour=(l/(60*60*1000)-day*24);     
  569.             
  570.           long min=((l/(60*1000))-day*24*60-hour*60);     
  571.             
  572.           long s=(l/1000-day*24*60*60-hour*60*60-min*60);    
  573.              
  574.           System.out.println(""+day+"天"+hour+"小时"+min+"分"+s+"秒");     
  575. catch (ParseException e) {   
  576. // TODO Auto-generated catch block   
  577. e.printStackTrace();   
  578. }     
  579.      }   
  580.       
  581.   
  582.     /**  
  583.      *  
  584.      * @Description:获取时间的分数  
  585.      * @param date   
  586.      * @ReturnType int  
  587.      * @author:  
  588.      * @Created 2012 2012-10-29上午11:45:13  
  589.      */   
  590.     public static int getTimeMinute(Date date) {       
  591.     Calendar calender = Calendar.getInstance();   
  592.     calender.setTime(date);   
  593.     return calender.get(Calendar.MINUTE);   
  594.     }   
  595.       
  596. /**  
  597. * 给出时间分格式转换时间单位  
  598. * @param time  String ; 12:20:30  
  599. * @return String ;12h20m30s  
  600. */   
  601. public static String getTimeUnit(String time){   
  602. time = time.replaceFirst(":""h");   
  603. time = time.replaceFirst(":""m")+"s";   
  604. return time;   
  605. }   
  606.   
  607.   
  608.   /**   
  609.     * 根据当前日期及增加天数得到相应日期   
  610.     * @param s   
  611.     * @param n   
  612.     * @return   
  613.     * @throws Exception   
  614.     */    
  615.     public static String addDay(String s,int n) {            
  616.     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");     
  617.     Calendar cd = Calendar.getInstance();     
  618.     try{         
  619.     cd.setTime(sdf.parse(s));     
  620.     cd.add(Calendar.DATE, n);     
  621.     }catch(Exception e){}     
  622.     return sdf.format(cd.getTime());     
  623.    }   
  624. }