当前位置: 移动技术网 > IT编程>开发语言>Java > java分割日期时间段代码

java分割日期时间段代码

2019年07月22日  | 移动技术网IT编程  | 我要评论

华池在线,工资证明怎么开,红双喜香烟价格

本文实例为大家分享了java切割日期时间段代码,供大家参考,具体内容如下

/**
 * @author dy
 * @since 2016-09-18 & jdk 1.8.0_91
 */
public class datecalculate {
  static logger logger = loggerfactory.getlogger(datecalculate.class);

  /**
   * 切割时间段
   *
   * @param datetype 交易类型 m/d/h/n -->每月/每天/每小时/每分钟
   * @param start yyyy-mm-dd hh:mm:ss
   * @param end  yyyy-mm-dd hh:mm:ss
   * @return
   */
  public static list<string> cutdate(string datetype, string start, string end) {
    try {
      simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");
      date dbegin = sdf.parse(start);
      date dend = sdf.parse(end);
      return finddates(datetype, dbegin, dend);
    } catch (exception e) {
      logger.error(e.getmessage(), e);
    }
    return null;
  }

  public static list<string> finddates(string datetype, date dbegin, date dend) throws exception {
    list<string> listdate = new arraylist<>();
    calendar calbegin = calendar.getinstance();
    calbegin.settime(dbegin);
    calendar calend = calendar.getinstance();
    calend.settime(dend);
    while (calend.after(calbegin)) {
      switch (datetype) {
        case "m":
          calbegin.add(calendar.month, 1);
          break;
        case "d":
          calbegin.add(calendar.day_of_year, 1);break;
        case "h":
          calbegin.add(calendar.hour, 1);break;
        case "n":
          calbegin.add(calendar.second, 1);break;
      }
      if (calend.after(calbegin))
        listdate.add(new simpledateformat("yyyy-mm-dd hh:mm:ss").format(calbegin.gettime()));
      else
        listdate.add(new simpledateformat("yyyy-mm-dd hh:mm:ss").format(calend.gettime()));
    }
    return listdate;
  }


  public static void main(string[] args) {
    string start = "2016-02-01 00:00:00";
    string end = "2016-03-02 00:00:00";
    list<string> list = cutdate("d", start, end);
    for (string str :list){
      system.out.println(str);
    }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网