简体   繁体   中英

i have a list of dates and i want to use then in pairs as from and to in a pja query until all dates have been used end in java

i have a list of dates and i want to use them in pairs as datefrom and dateto in a query until all dates have been used in java. date list 2017-06-07, 2017-06-08,2017-06-09,2017-06-10,2017-06-11 etc. i want to use them in pairs as illustrated on the image attached. I want these dates to be paired inside a loop until all dates have been used

List<String> times = Arrays.asList("2017-06-07", "2017-06-08","2017-06-
09","2017-06-10","2017-06-11");
for(String date: times){
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
    LocalDateTime dateFrom = LocalDateTime.parse(date, formatter);
    LocalDateTime dateTo = LocalDateTime.parse(times.get(count+1), formatter);
    List<xxx> rec = 
        xxxxxxRepository.xxxxxDateGreaterThanEqualAndDateLessThanEqual(1,
            23,dateFrom, dateTo);
}

Do you mean like this?

List<String> times = Arrays.asList("2017-06-07", "2017-06-08","2017-06-09","2017-06-10","2017-06-11");
System.out.println("from         to");
String prev = null;
for (String date : times) {
    if (prev != null)
        System.out.println(prev + "   " + date);
    prev = date;
}

Output

from         to
2017-06-07   2017-06-08
2017-06-08   2017-06-09
2017-06-09   2017-06-10
2017-06-10   2017-06-11

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM