java比較兩個日期之間月差值,日差值

昨天開發的時候,遇到一個需求,需要根據用戶輸入的兩個日期,計算出這兩個日期之間的所有間隔的月份,包括開始和結束日期,然後使用這些日期分別進行查詢。搜了一下,沒找到現成的例子。於是自己寫了一個。

package com.inspur.servlet;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Test {

	public static void main(String[] args) throws Exception {
		compareTwoData4Days("2013-02-22", "2014-01-23");
		System.err.println("-------------------");
		compareTwoData4Months("2013-02-22", "2014-01-23");
	}

	/**
	 * 
	 * @creator:李小龍
	 * @createDate:2014-9-4上午10:16:09
	 * @discription:比較兩個日期的差值,精確到“月”。包括開始和結束日期。
	 * @params:
	 * @return:
	 * @modifyInfo:
	 */
	static void compareTwoData4Months(String begin, String end)
			throws Exception {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
		Date bd = df.parse(begin);
		Date ed = df.parse(end);
		Calendar c = Calendar.getInstance();
		c.setTime(bd);
		c.add(Calendar.MONTH, -1);
		Date nd = c.getTime();
		while (nd.before(ed)) {
			c.setTime(nd);
			c.add(Calendar.MONTH, 1);
			nd = c.getTime();
			int year = c.get(Calendar.YEAR);
			String month = (c.get(Calendar.MONTH) + 1) + "";
			if (month.length() == 1)
				month = "0" + month;
			System.err.println(year + "-" + month);
		}
	}

	/**
	 * 
	 * @creator:李小龍
	 * @createDate:2014-9-4上午10:20:03
	 * @discription:比較兩個日期的差值,精確到“天”。包括開始和結束日期。
	 * @params:
	 * @return:
	 * @modifyInfo:
	 */
	public static void compareTwoData4Days(String begin, String end)
			throws Exception {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date bd = df.parse(begin);
		Date ed = df.parse(end);
		Calendar c = Calendar.getInstance();
		c.setTime(bd);
		c.add(Calendar.DAY_OF_MONTH, -1);
		Date nd = c.getTime();
		while (nd.before(ed)) {
			c.setTime(nd);
			c.add(Calendar.DAY_OF_MONTH, 1);
			nd = c.getTime();
			int year = c.get(Calendar.YEAR);
			String month = (c.get(Calendar.MONTH) + 1) + "";
			String day = c.get(Calendar.DATE) + "";
			if (month.length() == 1)
				month = "0" + month;
			if (day.length() == 1)
				day = "0" + day;
			System.err.println(year + "-" + month + "-" + day);
		}
	}
}

兩個方法,分別是比較月份和日。進入兩個while循環前,我先對當前的時間減一,這樣可保證當前日期也被算進去。

只是簡單輸出了一下,如果需要使用,則放到有序list集合中即可,如果不需要開始和結束日期,直接使用list remove掉即可。

輸出結果:

2013-02-22
2013-02-23
2013-02-24
2013-02-25
2013-02-26
2013-02-27
2013-02-28
2013-03-01
2013-03-02
2013-03-03
2013-03-04
2013-03-05
2013-03-06
2013-03-07
2013-03-08
2013-03-09
2013-03-10
2013-03-11
2013-03-12
2013-03-13
2013-03-14
2013-03-15
2013-03-16
2013-03-17
2013-03-18
2013-03-19
2013-03-20
2013-03-21
2013-03-22
2013-03-23
2013-03-24
2013-03-25
2013-03-26
2013-03-27
2013-03-28
2013-03-29
2013-03-30
2013-03-31
2013-04-01
2013-04-02
2013-04-03
2013-04-04
2013-04-05
2013-04-06
2013-04-07
2013-04-08
2013-04-09
2013-04-10
2013-04-11
2013-04-12
2013-04-13
2013-04-14
2013-04-15
2013-04-16
2013-04-17
2013-04-18
2013-04-19
2013-04-20
2013-04-21
2013-04-22
2013-04-23
2013-04-24
2013-04-25
2013-04-26
2013-04-27
2013-04-28
2013-04-29
2013-04-30
2013-05-01
2013-05-02
2013-05-03
2013-05-04
2013-05-05
2013-05-06
2013-05-07
2013-05-08
2013-05-09
2013-05-10
2013-05-11
2013-05-12
2013-05-13
2013-05-14
2013-05-15
2013-05-16
2013-05-17
2013-05-18
2013-05-19
2013-05-20
2013-05-21
2013-05-22
2013-05-23
2013-05-24
2013-05-25
2013-05-26
2013-05-27
2013-05-28
2013-05-29
2013-05-30
2013-05-31
2013-06-01
2013-06-02
2013-06-03
2013-06-04
2013-06-05
2013-06-06
2013-06-07
2013-06-08
2013-06-09
2013-06-10
2013-06-11
2013-06-12
2013-06-13
2013-06-14
2013-06-15
2013-06-16
2013-06-17
2013-06-18
2013-06-19
2013-06-20
2013-06-21
2013-06-22
2013-06-23
2013-06-24
2013-06-25
2013-06-26
2013-06-27
2013-06-28
2013-06-29
2013-06-30
2013-07-01
2013-07-02
2013-07-03
2013-07-04
2013-07-05
2013-07-06
2013-07-07
2013-07-08
2013-07-09
2013-07-10
2013-07-11
2013-07-12
2013-07-13
2013-07-14
2013-07-15
2013-07-16
2013-07-17
2013-07-18
2013-07-19
2013-07-20
2013-07-21
2013-07-22
2013-07-23
2013-07-24
2013-07-25
2013-07-26
2013-07-27
2013-07-28
2013-07-29
2013-07-30
2013-07-31
2013-08-01
2013-08-02
2013-08-03
2013-08-04
2013-08-05
2013-08-06
2013-08-07
2013-08-08
2013-08-09
2013-08-10
2013-08-11
2013-08-12
2013-08-13
2013-08-14
2013-08-15
2013-08-16
2013-08-17
2013-08-18
2013-08-19
2013-08-20
2013-08-21
2013-08-22
2013-08-23
2013-08-24
2013-08-25
2013-08-26
2013-08-27
2013-08-28
2013-08-29
2013-08-30
2013-08-31
2013-09-01
2013-09-02
2013-09-03
2013-09-04
2013-09-05
2013-09-06
2013-09-07
2013-09-08
2013-09-09
2013-09-10
2013-09-11
2013-09-12
2013-09-13
2013-09-14
2013-09-15
2013-09-16
2013-09-17
2013-09-18
2013-09-19
2013-09-20
2013-09-21
2013-09-22
2013-09-23
2013-09-24
2013-09-25
2013-09-26
2013-09-27
2013-09-28
2013-09-29
2013-09-30
2013-10-01
2013-10-02
2013-10-03
2013-10-04
2013-10-05
2013-10-06
2013-10-07
2013-10-08
2013-10-09
2013-10-10
2013-10-11
2013-10-12
2013-10-13
2013-10-14
2013-10-15
2013-10-16
2013-10-17
2013-10-18
2013-10-19
2013-10-20
2013-10-21
2013-10-22
2013-10-23
2013-10-24
2013-10-25
2013-10-26
2013-10-27
2013-10-28
2013-10-29
2013-10-30
2013-10-31
2013-11-01
2013-11-02
2013-11-03
2013-11-04
2013-11-05
2013-11-06
2013-11-07
2013-11-08
2013-11-09
2013-11-10
2013-11-11
2013-11-12
2013-11-13
2013-11-14
2013-11-15
2013-11-16
2013-11-17
2013-11-18
2013-11-19
2013-11-20
2013-11-21
2013-11-22
2013-11-23
2013-11-24
2013-11-25
2013-11-26
2013-11-27
2013-11-28
2013-11-29
2013-11-30
2013-12-01
2013-12-02
2013-12-03
2013-12-04
2013-12-05
2013-12-06
2013-12-07
2013-12-08
2013-12-09
2013-12-10
2013-12-11
2013-12-12
2013-12-13
2013-12-14
2013-12-15
2013-12-16
2013-12-17
2013-12-18
2013-12-19
2013-12-20
2013-12-21
2013-12-22
2013-12-23
2013-12-24
2013-12-25
2013-12-26
2013-12-27
2013-12-28
2013-12-29
2013-12-30
2013-12-31
2014-01-01
2014-01-02
2014-01-03
2014-01-04
2014-01-05
2014-01-06
2014-01-07
2014-01-08
2014-01-09
2014-01-10
2014-01-11
2014-01-12
2014-01-13
2014-01-14
2014-01-15
2014-01-16
2014-01-17
2014-01-18
2014-01-19
2014-01-20
2014-01-21
2014-01-22
2014-01-23
-------------------
2013-02
2013-03
2013-04
2013-05
2013-06
2013-07
2013-08
2013-09
2013-10
2013-11
2013-12
2014-01


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章