Here you can find the source of isDateSortedAsc(List
Parameter | Description |
---|---|
dates | List of dates in strong format based on the passed format |
format | Expected string format of the dates |
public static boolean isDateSortedAsc(List<String> dates, String format)
//package com.java2s; //License from project: Apache License import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Locale; public class Main { /**/* w w w . ja v a 2 s .c om*/ * Determines if the list of dates is sorted in ascending order * * @param dates * {@link List} of dates in strong format based on the passed * format * @param format * Expected string format of the dates * @return true if in ascending order */ public static boolean isDateSortedAsc(List<String> dates, String format) { boolean sorted = true; Iterator<String> iList = dates.iterator(); String first = iList.next(); try { Date curr = new SimpleDateFormat(format, Locale.ENGLISH) .parse(first); while (iList.hasNext()) { String next = iList.next(); Date nextDate = new SimpleDateFormat(format, Locale.ENGLISH) .parse(next); if (curr.compareTo(nextDate) > 0) { sorted = false; break; } } } catch (ParseException e) { e.printStackTrace(); } return sorted; } }