尝试解决问题难题,但没有太多运气。
我有一个日期列表和一个传递给函数的Date对象:
function void dateSorting(List<Date> dateList, Date newDate);
基于newDate我需要找出这个newDate适合的列表的哪两个索引。
所以例如。
如果日期是2015年1月3日且列表包含:
INDEX, VALUE
0 , 5/1/2015
1 , 20/2/2015
2 , 15/3/2015
3 , 11/4/2015
然后功能将打印/返回1和2以及相应的日期20/2/2015&amp; 15/3/2015。
我一直在网上搜索伪代码和例子,但没有运气。我最近的是与newDate最接近的日期,这对我没有帮助。
感谢任何帮助。
答案 0 :(得分:1)
在这里,我找到了一个。
void dateSorting(List<Date> dateList, Date newDate) {
for (int i = 0; i < dateList.size(); i++) {
if (newDate.before(dateList.get(i))) {
if (i != 0)
System.out.println("Previous index:" + (i - 1));
System.out.println("Next index:" + i);
break;
} else if (i == dateList.size() - 1) {
System.out.println("Previous index:" + i);
}
}
}
我测试了我的代码:
public static void main(String[] args) throws ParseException {
List<Date> dateList = new ArrayList<Date>();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
dateList.add(sdf.parse("5/1/2015"));
dateList.add(sdf.parse("20/2/2015"));
dateList.add(sdf.parse("15/3/2015"));
dateList.add(sdf.parse("11/4/2015"));
System.out.println("Testing with 1/3/2015.");
dateSorting(dateList, sdf.parse("1/3/2015"));
System.out.println("Testing with 1/1/2015.");
dateSorting(dateList, sdf.parse("1/1/2015"));
System.out.println("Testing with 12/4/2015.");
dateSorting(dateList, sdf.parse("12/4/2015"));
}
我得到了以下结果:
Testing with 1/3/2015.
Previous index:1
Next index:2
Testing with 1/1/2015.
Next index:0
Testing with 12/4/2015.
Previous index:3
我相信这会对你有所帮助。
答案 1 :(得分:0)
将 17/11/2015 替换为Date.toString()
(日期迭代器)
public static void main(String []args){
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try {
date = sdf.parse("17/11/2015");
Calendar cal = Calendar.getInstance();
cal.setTime(date);
System.out.println(cal.getTime());
cal.add(Calendar.DAY_OF_MONTH, 1);
System.out.println(cal.getTime());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
答案 2 :(得分:0)
尝试以下代码:
long current = newDate.getTime();
boolean flag = false;
for (int i =1; i < dateList.size(); i++){
long a = dateList.get(i-1).getTime(); long b = dateList.get(i).getTime();
if ( (a > current && b < current) || (a < current && b > current){
//newDate is in between index i-1 and i, set flag to true and do your stuff.
}
if(!flag) { //newDate did not fit between any indexes.
}
}
您应该了解日历和日期API。最好使用Calender API,因为不推荐使用Date中的大多数方法。
答案 3 :(得分:0)
我认为这是你真正的期望:
首先,您需要创建一个实现Comparator的类来对日期列表进行排序:
public class DateSort implements Comparator<Date> {
@Override
public int compare(Date d1, Date d2) {
return (d1.getTime() > d2.getTime() ? 1 : -1);
}
}
然后你可以实现一个算法来获得壁橱最大日期和最近的最短日期,如下所示:
public class CompareDate {
public void compare(String oparation, List<Date> dateList, SimpleDateFormat sdf, Comparator<Date> comparator)
throws ParseException {
Collections.sort(dateList, comparator);
//using joda time api
DateTime givenDate = new DateTime(sdf.parse("01/03/2015"));
Days diff = Days.days(0);
OUTER:
for (Date date : dateList) {
Days newDiff = Days.daysBetween(new DateTime(date), givenDate);
switch (oparation) {
case "max-closet":
if (newDiff.getDays() < diff.getDays()) {
diff = newDiff;
System.out.println(oparation + " - " + sdf.format(date));
break OUTER;
}
break;
case "min-closet":
if (newDiff.getDays() > diff.getDays()) {
diff = newDiff;
System.out.println(oparation + " - " + sdf.format(date));
break OUTER;
}
break;
}
}
}
public static void main(String[] args) throws ParseException {
List<Date> dateList = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
dateList.add(sdf.parse("05/01/2015"));
dateList.add(sdf.parse("20/02/2015"));
dateList.add(sdf.parse("15/03/2015"));
dateList.add(sdf.parse("11/04/2015"));
new CompareDate().compare("max-closet", dateList, sdf, new DateSort());
new CompareDate().compare("min-closet", dateList, sdf, new DateSort().reversed());
//I m reversing the date list so I can sort dates to descending order.
}
}
在这里,我使用joda time api来获取两个日期之间的差异。