zoukankan      html  css  js  c++  java
  • Java获取两个指定日期之间的所有月份(查询两个月份之间的相隔)

    Java获取两个指定日期之间的所有月份

    String y1 = "2016-02";// 开始时间
    String y2 = "2019-12";// 结束时间

    try {
    Date startDate = new SimpleDateFormat("yyyy-MM").parse(y1);
    Date endDate = new SimpleDateFormat("yyyy-MM").parse(y2);

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(startDate);
    // 获取开始年份和开始月份
    int startYear = calendar.get(Calendar.YEAR);
    int startMonth = calendar.get(Calendar.MONTH);
    // 获取结束年份和结束月份
    calendar.setTime(endDate);
    int endYear = calendar.get(Calendar.YEAR);
    int endMonth = calendar.get(Calendar.MONTH);
    //
    List<String> list = new ArrayList<String>();
    for (int i = startYear; i <= endYear; i++) {
    String date = "";
    if (startYear == endYear) {
    for (int j = startMonth; j <= endMonth; j++) {
    if (j < 9) {
    date = i + "-0" + (j + 1);
    } else {
    date = i + "-" + (j + 1);
    }
    list.add(date);
    }

    } else {
    if (i == startYear) {
    for (int j = startMonth; j < 12; j++) {
    if (j < 9) {
    date = i + "-0" + (j + 1);
    } else {
    date = i + "-" + (j + 1);
    }
    list.add(date);
    }
    } else if (i == endYear) {
    for (int j = 0; j <= endMonth; j++) {
    if (j < 9) {
    date = i + "-0" + (j + 1);
    } else {
    date = i + "-" + (j + 1);
    }
    list.add(date);
    }
    } else {
    for (int j = 0; j < 12; j++) {
    if (j < 9) {
    date = i + "-0" + (j + 1);
    } else {
    date = i + "-" + (j + 1);
    }
    list.add(date);
    }
    }

    }

    }

    // 所有的月份已经准备好
    //System.out.println(list);
    for(int i = 0;i < list.size();i++){
    System.out.println(list.get(i));
    }

    } catch (Exception e) {
    e.printStackTrace();
    }

    转自:https://www.cnblogs.com/beanbag/p/9719907.html  尊重原创(让更多的人看到)

  • 相关阅读:
    DRF之url注册器组件
    序列化组件的使用及接口设计和优化
    Django 内置字段
    Django 的 ModelForm组件
    Django组件 中间件
    csrf
    django使用redis做缓存
    微信消息推送
    自定制serilazry字段
    小知识,大智慧(restframework 拾忆)
  • 原文地址:https://www.cnblogs.com/490889763-qq/p/14633136.html
Copyright © 2011-2022 走看看