java两个日期的差值,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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值