java根据开始时间结束时间计算中间间隔日期的实例代码

具体代码如下所述:

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Calendar;

import java.util.Date;

import java.util.List;

public class Test {

public static List<String> findDates(String stime, String etime)

throws ParseException {

List<String> allDate = new ArrayList();

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

Date dBegin = sdf.parse(stime);

Date dEnd = sdf.parse(etime);

allDate.add(sdf.format(dBegin));

Calendar calBegin = Calendar.getInstance();

// 使用给定的 Date 设置此 Calendar 的时间

calBegin.setTime(dBegin);

Calendar calEnd = Calendar.getInstance();

// 使用给定的 Date 设置此 Calendar 的时间

calEnd.setTime(dEnd);

// 测试此日期是否在指定日期之后

while (dEnd.after(calBegin.getTime())) {

// 根据日历的规则,为给定的日历字段添加或减去指定的时间量

calBegin.add(Calendar.DAY_OF_MONTH, 1);

allDate.add(sdf.format(calBegin.getTime()));

}

return allDate;

}

public static void main(String[] args) {

//测试数据

String stime = "2019-05-01";

String etime = "2019-05-05";

//集合中包含2019-05-01/2019-05-05,不需要可去除

List<String> list = new ArrayList<>();

try {

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

long s = Long.valueOf(sdf.parse(stime).getTime());

long e = Long.valueOf(sdf.parse(etime).getTime());

//只有结束时间大于开始时间时才进行查询

if(s<e) {

list = findDates(stime, etime);

}

} catch (ParseException e) {

e.printStackTrace();

}

for(String time : list) {

System.out.println(time);

}

System.out.println("间隔天数:" + list.size());

}

}

ps:下面看下java根据开始时间和结束时间,计算中间天数,并打印

import java.text.SimpleDateFormat;

import java.util.Date;

public class Calcdate {

static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

static SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");

public static void main(String[] args)throws Exception{

Date beginTime = sdf.parse("2015-04-23 12:32:44");

// Date endTime = sdf.parse("2015-04-23 14:56:44");

Date endTime = sdf.parse("2015-04-29 14:56:44");

long diff = (endTime.getTime() - beginTime.getTime()) / (86400 * 1000);

System.out.println("diff = " + diff);

Date sb = null;

Date se = null;

if(diff == 0){

sb = beginTime;

se = endTime;

String ss = String.format("%d> Sb=%s, Se=%s", 1, Coder.DateTimeToStr(sb), Coder.DateTimeToStr(se));

System.out.println(ss);

}else {

int i = 0;

while (i <= diff) {

if (i == 0) {

sb = beginTime;

se = sdf.parse(ymd.format(sb) + " 23:59:59");

} else if (i == diff) {

sb = sdf.parse(ymd.format(endTime) + " 00:00:00");

se = endTime;

} else {

se = new Date(beginTime.getTime() + i * 24 * 60 * 60 * 1000);

sb = sdf.parse(ymd.format(se) + " 00:00:00");

se = sdf.parse(ymd.format(se) + " 23:59:59");

}

String ss = String.format("%d> Sb=%s, Se=%s", i, Coder.DateTimeToStr(sb), Coder.DateTimeToStr(se));

System.out.println(ss);

i++;

}

}

}

}

diff = 6

0> Sb=2015-04-23 12:32:44, Se=2015-04-23 23:59:59

1> Sb=2015-04-24 00:00:00, Se=2015-04-24 23:59:59

2> Sb=2015-04-25 00:00:00, Se=2015-04-25 23:59:59

3> Sb=2015-04-26 00:00:00, Se=2015-04-26 23:59:59

4> Sb=2015-04-27 00:00:00, Se=2015-04-27 23:59:59

5> Sb=2015-04-28 00:00:00, Se=2015-04-28 23:59:59

6> Sb=2015-04-29 00:00:00, Se=2015-04-29 14:56:44

总结

以上所述是小编给大家介绍的java根据开始时间结束时间计算中间间隔日期的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

以上是 java根据开始时间结束时间计算中间间隔日期的实例代码 的全部内容, 来源链接: utcz.com/z/339442.html

回到顶部