2017-10-06 98 views
0

我嘗試獲取2日期之間的日期列表。但是,列表中的所有值都是開始日期。獲取2日期之間的日期列表有錯誤

例如

datefrom = 2017年6月10日

dateto = 2017年10月10日

==>列表的所有的值是datefrom(2017年6月10日)

預計---------------------------------------實際

日期[0] = 06-10-2017 -------------------日期[0] = 06-10-2017

日期[1] = 07-10-2017 -------------------日期[1] = 06-10-2017

日期[2] = 08-10-2017 ------------------- dates [2] = 06-10-2017

日期[3] = 09-10-2017- ------------------ dates [3] = 06-10-2017

日期[4] = 10-10-2017 -------- -----------日期[4] = 06-10-2017

任何幫助,將不勝感激。

public class fragTable extends Fragment{ 
... 
private String mdateFrom = "06-10-2017"; 
private String mdateTo = "10-10-2017"; 

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    List<String> dates = new ArrayList<String>();  
    dates = getDaysBetweenDates(stringToDaTe(mdateFrom),stringToDaTe(mdateTo)); 
} 

public List<String> getDaysBetweenDates(Date startdate, Date enddate) 
{ 
    List<String> dates = new ArrayList<String>(); 
    Calendar calendar = new GregorianCalendar(); 
    calendar.setTime(startdate); 

    while (calendar.getTime().before(enddate)) 
    { 
     Date result = calendar.getTime(); 
     dates.add(datetoString(result)); 
     calendar.add(Calendar.DATE, 1); 
    } 
    return dates; 
} 
//Convert String to Date 
public Date stringToDaTe(String dateString) 
{ 
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); 
    Date date = new Date(); 
    try { 
     date = sdf.parse(dateString); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 
    return date; 
} 
    //Convert Date to String 
    public String datetoString(Date date) 
{ 
    SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy"); 
    Date today = Calendar.getInstance().getTime(); 
    String result = df.format(today); 
    return result; 
} 
} 
+0

發佈錯誤比 –

+0

什麼是datetoString? – mikep

+0

你的問題不清楚,預期輸出是什麼?你只需要在排除開始日期和結束日期之間的日期? – Count

回答

4

檢查dateToString方法:

public String datetoString(Date date) 
{ 
    SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy"); 
    Date today = Calendar.getInstance().getTime(); 
    String result = df.format(today);  // <= You're actually formatting "today" date, not "date" 
    return result; 
} 

通過改變該行

String result = df.format(date); 

你的問題應該得到解決

1

這裏是獲取兩個日期之間日期的代碼。

public class test { 
    private static String mdateFrom = "06-10-2017"; 
    private static String mdateTo = "10-10-2017"; 

    public static void main(String[] args) { 
     List<String> dates = getDaysBetweenDates(stringToDaTe(mdateFrom), stringToDaTe(mdateTo)); 
     System.out.println(dates); 
    } 

    public static List<String> getDaysBetweenDates(Date startdate, Date enddate) { 
     List<String> dates = new ArrayList<String>(); 
     Calendar calendar = new GregorianCalendar(); 
     calendar.setTime(startdate); 

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

    public static Date stringToDaTe(String dateString) { 
     SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); 
     Date date = new Date(); 
     try { 
      date = sdf.parse(dateString); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return date; 
    } 

    public static String formatDate(Date result) { 
     String date = ""; 
     DateFormat df = new SimpleDateFormat("dd-MM/yyyy"); 
     date = df.format(result); 
     return date; 
    } 
} 

,輸出是:

[06-10/2017, 07-10/2017, 08-10/2017, 09-10/2017] 
0

不知道如何打印您的輸出我猜你使用datetoString()

Date today = Calendar.getInstance().getTime(); 
String result = df.format(today); 
return result; // returns always today, not given date 

也許它應該有

String result = df.format(date) // the input ; 
return result; 
0

推薦使用LocalDate從Java 8 代碼變得更簡單,輕便無需鑄造從字符串來回。另外你可以節省潛在的時區問題

public List<LocalDate> getDaysBetweenDates(Date startdate, Date enddate) 
{ 
    LocalDate dateFrom =stringToDaTe(mdateFrom).toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); 
    LocalDate dateTo =stringToDaTe(mdateTo).toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); 

    long days = Duration.between(dateFrom.atStartOfDay(), dateTo.atStartOfDay()).toDays(); 
    List<LocalDate> resultList= new ArrayList<LocalDate>(); 
    long i=0; 
    do { 
     resultList.add(dateFrom); 
     dateFrom = dateFrom.plusDays(1); 
     i++; 

    } while (i < days+1); 
    return resultList;  
} 
相關問題