欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-22 20:26:58
具体代码如下所述: import java.text.parseexception; import java.text.simpledateformat; i...

具体代码如下所述:

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根据开始时间结束时间计算中间间隔日期的实例代码,希望对大家有所帮助