【问题标题】:Get dates array within a date range获取日期范围内的日期数组
【发布时间】:2019-02-23 08:20:23
【问题描述】:

如何将日期之间的日期循环存储到数组中?

示例 1:
第一个日期 = 26/9/2018
最后日期 = 2018 年 9 月 30 日

我想像下面这样循环存储日期:

new Date["26/9/2018","27/9/2018","28/9/2018","29/9/2018","30/9/2018"]

类似:

for(int i = 0; FirstDate < LastDate; i++){
   //add array here by loop
}

所以当我选择如下时,我得到了new Date[4]

new Date[3],它将显示 29/9/2018

【问题讨论】:

  • 使用 jodatime 库进行日期比较
  • 将您的日期转换为毫秒,然后进行比较。

标签: android arrays for-loop


【解决方案1】:

我建议您使用 Jake Warthon 的 LocalDate api。日期 api 在 android 中不是很可靠。顺便说一句,试试这个解决方案:

public ArrayList<LocalDate> getDateLoop() {
        LocalDate startDate = LocalDate.of(2018, 9, 26);
        LocalDate endDate = LocalDate.of(2018, 9, 30);
        ArrayList<LocalDate> dates = new ArrayList<>();

        for (int i = startDate.getDayOfMonth(); i <= endDate.getDayOfMonth(); i++) {
            LocalDate dateToAdd = LocalDate.of(startDate.getYear(), startDate.getMonth(), i);
            dates.add(dateToAdd);
        }
        return dates;
    }

这是图书馆的链接:https://github.com/JakeWharton/ThreeTenABP

【讨论】:

  • 谢谢先生,但这仅适用于26api及以上。如何将日历日期放入LocalDate.of()?
  • @TommyChong Android api 中的 LocalDate 类可用于 api 26 及更高版本,Jake Warthon 在这里github.com/JakeWharton/ThreeTenABP 的移植是复古兼容的。包括图书馆并尝试。编译'com.jakewharton.threetenabp:threetenabp:1.1.0'
  • @Nicole Gallazzi 是的,它很有用,谢谢你让我知道这个库.. 但我的代码很难实现。
【解决方案2】:

你可以试试这个:

        List<Date> dates = new ArrayList<Date>();

        String start_date ="26/9/2018";
        String end_date ="30/9/2018";

        DateFormat formatter ;

        formatter = new SimpleDateFormat("dd/M/yyyy");
        Date  startDate = null;
        Date  endDate = null;
        try {
            startDate = formatter.parse(start_date);
            endDate = formatter.parse(end_date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        long interval = 24*1000 * 60 * 60;
        long endTime =endDate.getTime() ;
        long curTime = startDate.getTime();
        while (curTime <= endTime) {
            dates.add(new Date(curTime));
            curTime += interval;
        }
        for(int i=0;i<dates.size();i++){
            String result = formatter.format(dates.get(i));
        }

可能不是最佳的,但测试成功,希望有帮助。

【讨论】:

    【解决方案3】:

    只需使用此代码:

    public Date[] getDateLoop() {
      String str_start_date = "26/9/2018";
      String str_end_date = "30/9/2018";
      SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
      //Date date = sdf.parse(dateStr);
      Date start_date = null;
      try {
          start_date = sdf.parse(str_start_date);
      } catch (ParseException e) {
          e.printStackTrace();
      }
    
      Date end_date = null;
      try {
          end_date = sdf.parse(str_end_date);
      } catch (ParseException e) {
          e.printStackTrace();
      }
    
      long diff = TimeUnit.DAYS.convert(end_date.getTime() - start_date.getTime(), TimeUnit.MILLISECONDS);
    
      Date[] array_date = new Date[(int) diff + 1];
      for (int i = 0; i <= diff; i++) {
          Date temp_date = start_date;
          Calendar c = Calendar.getInstance();
          c.setTime(temp_date);
          c.add(Calendar.DATE, i);
          temp_date = c.getTime();
          array_date[i] = temp_date;
      }
      return array_date;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-03-02
      • 2014-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-01
      • 1970-01-01
      相关资源
      最近更新 更多