-1

我有一个对象列表,其中包含不同航班的信息,每个航班都有一个日期。我试图完成的是让用户选择一个日期,然后我会遍历列表以在该日期返回航班,但也会提前几天和之后。

怎么做?

逻辑 - - - - - -

获取此日期的航班,然后获取此日期前后的航班

4

3 回答 3

0

Iterate the List obtain the flight details and define a range of Date which you want to obtain

List<Flight> flights=new ArrayList<Flight>();
for(Flight f:flights)
 {
   Date d=f.getDate();
    if(d is within the desired range)
     {
        //your logic in here
     }
 }

If you want a logic from scratch what you can do is obtain the current time in millseconds and substract the milliseconds consisted in a day(i.e.. 86400000) from current time in milliseconds

 Calendar c= Calendar.getInstance();
 System.out.println(c.getTimeInMillis()-86400000);   // This will give the date one day before the current date
 Date d=new Date(c.getTimeInMillis()-86400000);
 System.out.println(d.toString());
于 2014-12-11T14:21:16.693 回答
0

当您说“在这个日期左右”时,您需要对其进行量化。日期前后多少天或几周?从列表中提取数据时,使用它来创建上限和下限。

于 2014-12-11T13:56:44.310 回答
0

您可以尝试将航班对象放入地图中,其中日期是键,值是该日期所有航班的列表。

这意味着在特定日期获得航班是 n(1) 并且假设您的日期范围是一个常数(x 天),您将进行额外的 2x n(1) 查找。

如果您的界限是无限的或可能非常大,那可能不是要走的路。

于 2014-12-11T14:01:43.043 回答