Java Date Value Check isDateSortedAsc(List dates, String format)

Here you can find the source of isDateSortedAsc(List dates, String format)

Description

Determines if the list of dates is sorted in ascending order

License

Apache License

Parameter

Parameter Description
dates List of dates in strong format based on the passed format
format Expected string format of the dates

Return

true if in ascending order

Declaration

public static boolean isDateSortedAsc(List<String> dates, String format) 

Method Source Code

//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;
    }
}

Related

  1. isDateFormatString(String s, String dateFormat, Locale locale)
  2. isDateFormatValid(final String pattern)
  3. isDateInRange(Date startDt, Date endDt, Date theDate)
  4. isDateInRange(Date toCheck, Date minRange, Date maxRange)
  5. IsDateOK(String date)
  6. isDateTwo(String value)
  7. isDateValid(String date, Locale locale)
  8. isDateValid(String dateString, String validFormat)
  9. isDateValid(String dateToCheck, String pattern)