我尝试获取两个日期之间的日期列表。但是,列表中的所有值都是开始日期。
例如
datefrom = 06-10-2017
dateto = 10-10-2017
==>列表的所有值都是datefrom(06-10-2017)
预期---------------------------------------实际
日期[0] = 06-10-2017 ------------------- dates [0] = 06-10-2017
日期[1] = 07-10-2017 ------------------- dates [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 ------------------- dates [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 :(得分: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 :(得分:1)
以下是获取2个日期之间日期的代码。
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]
答案 2 :(得分: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;
答案 3 :(得分:0)
推荐使用java 8中的LocalDate 代码变得更简单,轻量级,无需从String来回转换。另外,您可以避免潜在的时区问题
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;
}