2013-07-17 124 views
-1

在我的應用程序中,用戶應該從listview中選擇日期。問題是產生這個列表。比如我需要六月至八月(期間可能)2010 - 2013年之間或所有日期。有沒有任何方法可以獲取這些數據?如何獲取兩個日期之間的日期列表?

例子: 我需要2013年1月1日之間的日期 - 2013年1月10日

  1. 2013年1月1日
  2. 2013年2月1日
  3. 2013年3月1日
  4. 2013年4月1日
  5. 05.01.2013
  6. 06.01.2013
  7. 2013年7月1日
  8. 2013年8月1日
  9. 2013年9月1日
  10. 2013年1月10日

在此先感謝

+3

一個簡單的搜索給了我這個:http://stackoverflow.com/questions/12083053/how-to-get-a-list-of-dates-between-two-dates-in -java-how-to-include-exclude-st – zeroke

回答

3

對於列表你可以只是做:

public static List<LocalDate> datesBetween(LocalDate start, LocalDate end) { 
    List<LocalDate> ret = new ArrayList<LocalDate>(); 
    for (LocalDate date = start; !date.isAfter(end); date = date.plusDays(1)) { 
     ret.add(date); 
    } 
    return ret; 
} 

注意,將包括end。如果你想要排除的結束,只需將循環中的條件更改爲date.isBefore(end)即可。

如果你只需要一個Iterable<LocalDate>你可以編寫自己的類來做到這一點非常有效,而不是建立一個列表。如果你不介意一定程度的嵌套,你可以用一個匿名類來做到這一點。例如(未經測試):

public static Iterable<LocalDate> datesBetween(final LocalDate start, 
               final LocalDate end) { 
    return new Iterable<LocalDate>() { 
     @Override public Iterator<LocalDate> iterator() { 
      return new Iterator<LocalDate>() { 
       private LocalDate next = start; 

       @Override 
       public boolean hasNext() { 
        return !next.isAfter(end); 
       } 

       @Override 
       public LocalDate next() { 
        if (next.isAfter(end)) { 
         throw NoSuchElementException(); 
        } 
        LocalDate ret = next; 
        next = next.plusDays(1); 
        return ret; 
       } 

       @Override 
       public void remove() { 
        throw new UnsupportedOperationException(); 
       } 
      }; 
     } 
    }; 
} 
+0

有沒有辦法重複數週,而不是幾天? – Avan

+0

'date.plusDays(7)'? – Amadan

+0

@Amadan:就像Amadan說的那樣。 –

2

,而結束日期之前以一天創建起始日期,增量環。

有許多堆棧溢出帖子,可以告訴你如何「添加一天至今」。

3

使用DatePicker片段是這樣的:

private static class DatePickerFragment extends DialogFragment 
            implements DatePickerDialog.OnDateSetListener { 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // Use the current date as the default date in the picker 
     final Calendar c = Calendar.getInstance(); 
     int year = c.get(Calendar.YEAR); 
     int month = c.get(Calendar.MONTH); 
     int day = c.get(Calendar.DAY_OF_MONTH); 

     // Create a new instance of DatePickerDialog and return it 
     return new DatePickerDialog(getActivity(), this, year, month, day); 
    } 

    @Override 
    public void onDateSet(DatePicker view, int year, int monthOfYear, 
      int dayOfMonth) { 
     // Copy the Date to the EditText set. 
     dateValue = String.format("%04d", year) + "-" + String.format("%02d", monthOfYear + 1) + "-" + String.format("%02d", dayOfMonth); 
    } 

} 

這應該是更容易獲得在首位的日期。使用以下代碼日期範圍:

public static List<Date> dateInterval(Date initial, Date final) { 
    List<Date> dates = new ArrayList<Date>(); 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTime(initial); 

    while (calendar.getTime().before(final)) { 
     Date result = calendar.getTime(); 
     dates.add(result); 
     calendar.add(Calendar.DATE, 1); 
    } 

return dates; 
} 

乾杯!

學分:this

相關問題