zoukankan      html  css  js  c++  java
  • 切割时间工具

    个人博客网:https://wushaopei.github.io/    (你想要这里多有)

    /** * @ClassName SplitDateUtil * @Description TODO * @Author wushaopei * @Date 2019/11/3 23:23 * @Version 1.0 */ import org.junit.Test; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; /** * 切割时间工具类:把一个时间区间,按照月份拆成不同的区间 * @author huangyin * */ public class SplitDateUtil { public static void main(String[] args) { List<KeyValueForDate> list = SplitDateUtil.getKeyValueForDate("2015-08-23","2016-06-10"); // List<KeyValueForDate> list = SplitDateUtil.getKeyValueForDate("2017-08-23", "2017-08-30"); System.out.println("开始日期--------------结束日期"); for(KeyValueForDate date : list){ System.out.println(date.getStartDate()+"-----"+date.getEndDate()); } } public static List<KeyValueForDate> getKeyValueForDate(String startDate,String endDate) { List<KeyValueForDate> list = null; try { list = new ArrayList<KeyValueForDate>(); String firstDay = ""; String lastDay = ""; Date d1 = new SimpleDateFormat("yyyy-MM-dd").parse(startDate);// 定义起始日期 Date d2 = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);// 定义结束日期 Calendar dd = Calendar.getInstance();// 定义日期实例 dd.setTime(d1);// 设置日期起始时间 Calendar cale = Calendar.getInstance(); Calendar c = Calendar.getInstance(); c.setTime(d2); int startDay = d1.getDate();// 23 System.out.println("startDay = " + startDay); int endDay = d2.getDate();// 10 System.out.println("endDay = " + endDay); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); KeyValueForDate keyValueForDate = null; while (dd.getTime().before(d2)) {// 判断是否到结束日期 keyValueForDate = new KeyValueForDate(); cale.setTime(dd.getTime()); if(dd.getTime().equals(d1)){ cale.set(Calendar.DAY_OF_MONTH, dd .getActualMaximum(Calendar.DAY_OF_MONTH)); lastDay = sdf.format(cale.getTime()); keyValueForDate.setStartDate(sdf.format(d1)); keyValueForDate.setEndDate(lastDay); }else if(dd.get(Calendar.MONTH) == d2.getMonth() && dd.get(Calendar.YEAR) == c.get(Calendar.YEAR)){ cale.set(Calendar.DAY_OF_MONTH,1);//取第一天 firstDay = sdf.format(cale.getTime()); keyValueForDate.setStartDate(firstDay); keyValueForDate.setEndDate(sdf.format(d2)); }else { cale.set(Calendar.DAY_OF_MONTH,1);//取第一天 firstDay = sdf.format(cale.getTime()); cale.set(Calendar.DAY_OF_MONTH, dd .getActualMaximum(Calendar.DAY_OF_MONTH)); lastDay = sdf.format(cale.getTime()); keyValueForDate.setStartDate(firstDay); keyValueForDate.setEndDate(lastDay); } list.add(keyValueForDate); dd.add(Calendar.MONTH, 1);// 进行当前日期月份加1 } if(endDay<startDay){ keyValueForDate = new KeyValueForDate(); cale.setTime(d2); cale.set(Calendar.DAY_OF_MONTH,1);//取第一天 firstDay = sdf.format(cale.getTime()); keyValueForDate.setStartDate(firstDay); keyValueForDate.setEndDate(sdf.format(d2)); list.add(keyValueForDate); } } catch (ParseException e) { return null; } return list; } } class KeyValueForDate{ private String startDate;// 开始时间 private String endDate;// 结束时间 public String getStartDate() { return startDate; } public void setStartDate(String startDate) { this.startDate = startDate; } public String getEndDate() { return endDate; } public void setEndDate(String endDate) { this.endDate = endDate; } }
  • 相关阅读:
    数据分析师入门——用 Pandas 进行数据预处理:数据清洗与可视化
    hdu 1532 Dinic模板(小白书)
    二分图的最大匹配、完美匹配和匈牙利算法(转)
    HDU 1532 (Dinic算法)
    HDU 1532 Drainage Ditches EK算法 flod算法
    Edmonds_Karp 算法入门详解(转)
    UVa 10801
    Codeforces Round #359 (Div. 2)C
    Codeforces Round #358 (Div. 2)B. Alyona and Mex
    int long long范围
  • 原文地址:https://www.cnblogs.com/wushaopei/p/11978914.html
Copyright © 2011-2022 走看看