Java Parse Date parseDates(String query)

Here you can find the source of parseDates(String query)

Description

parse Dates

License

Apache License

Declaration

public static Date[] parseDates(String query) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

public class Main {
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy/MM/dd");
    private static final Pattern SPLIT_PATTERN = Pattern.compile("-");

    public static Date[] parseDates(String query) {
        String[] segments = SPLIT_PATTERN.split(query);
        List<Date> dates = new ArrayList<>();
        for (String s : segments) {
            Date date = parse(s.trim());
            if (date != null) {
                dates.add(date);/*from   w  w  w . j  a va 2s . c o  m*/
            }
        }
        return dates.toArray(new Date[dates.size()]);
    }

    private static Date parse(String source) {
        if (source.equalsIgnoreCase("today")) {
            return new Date();
        }
        if (source.equalsIgnoreCase("yesterday")) {
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DATE, -1);
            return cal.getTime();
        }
        try {
            return DATE_FORMAT.parse(source);
        } catch (ParseException e) {
            return null;
        }
    }
}

Related

  1. parseDateNewFormat(String sDate)
  2. parseDateNoTimeWithDelimit(String sDate, String delimit)
  3. parseDateOrTimeString(String aDateString, String aFormatString)
  4. parseDateRFC(String value)
  5. parseDateRfc822(String str)
  6. parseDateStrFormat(String strDate)
  7. parseDateString(String dateStr)
  8. parseDateString(String dateString)
  9. parseDateString(String pattern, String dateString)