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

网友投稿 333 2023-01-06


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 findDates(String stime, String etime)

throws ParseException {

List 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 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

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")VRJETlu;

// 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根据开始时间结束时间计算中间间隔日期的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!

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")VRJETlu;

// 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根据开始时间结束时间计算中间间隔日期的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:接口自动化框架搭建(python接口自动化框架搭建)
下一篇:做接口测试的电脑要求多大(现在电脑接口一般多少)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~