Java LocalDate Format doesParsedDateMatchText(LocalDate parsedDate, String text, Locale formatLocale)

Here you can find the source of doesParsedDateMatchText(LocalDate parsedDate, String text, Locale formatLocale)

Description

doesParsedDateMatchText, This compares the numbers in a parsed date, to the original text from which the date was parsed.

License

Open Source License

Declaration

static public boolean doesParsedDateMatchText(LocalDate parsedDate, String text, Locale formatLocale) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.Locale;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import java.time.LocalDate;

import java.time.format.DateTimeFormatter;

public class Main {
    /**/*from ww w . j a va  2  s.  c  om*/
     * doesParsedDateMatchText, This compares the numbers in a parsed date, to the original text
     * from which the date was parsed. Specifically this compares the day of the month and the year
     * of the parsed date to the text. On a technical note, this function is not aware of which
     * field is which, but that ambiguity does not prevent it from performing a successful
     * comparison on all tested cases. This will return true if the dates are a match, or otherwise
     * return false.
     *
     * Testing note: This function has been thoroughly tested and gives the proper result with all
     * valid and invalid dates in the years between -10000 and 10000 inclusive. Valid dates are
     * defined as those dates that are returned from the LocalDate class, when using the
     * localDate.plusDays(1) function. Invalid dates are defined as any of the following: The 31st
     * day of February, April, June, September, or November. The 30th day of February. Or the 29th
     * day of February on any year that is not a leap year.
     */
    static public boolean doesParsedDateMatchText(LocalDate parsedDate, String text, Locale formatLocale) {
        if (parsedDate == null || text == null) {
            return false;
        }
        text = text.toLowerCase();
        // This only matches numbers, and it does not include any hyphen "-".
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(text);
        ArrayList<String> unsignedNumbersFound = new ArrayList<String>();
        while (matcher.find()) {
            String foundString = matcher.group();
            foundString = forceNumberStringToTwoDigits(foundString);
            unsignedNumbersFound.add(foundString);
        }
        String parsedDayOfMonth = "" + parsedDate.getDayOfMonth();
        parsedDayOfMonth = forceNumberStringToTwoDigits(parsedDayOfMonth);
        boolean dayOfMonthFound = unsignedNumbersFound.remove(parsedDayOfMonth);

        DateTimeFormatter formatBC = DateTimeFormatter.ofPattern("G", formatLocale);
        String eraBCString = LocalDate.of(-100, 1, 1).format(formatBC).toLowerCase();
        if (parsedDate.getYear() < 1 && text.contains(eraBCString)) {
            String parsedYearForBC = "" + (parsedDate.getYear() - 1);
            parsedYearForBC = parsedYearForBC.replace("-", "");
            parsedYearForBC = forceNumberStringToTwoDigits(parsedYearForBC);
            boolean yearFoundForBC = unsignedNumbersFound.remove(parsedYearForBC);
            return yearFoundForBC && dayOfMonthFound;
        } else {
            String parsedYear = "" + parsedDate.getYear();
            parsedYear = parsedYear.replace("-", "");
            parsedYear = forceNumberStringToTwoDigits(parsedYear);
            boolean yearFound = unsignedNumbersFound.remove(parsedYear);
            return yearFound && dayOfMonthFound;
        }
    }

    /**
     * forceNumberStringToTwoDigits, This takes a string of digits, and forces it to be two digits
     * long. First any extra leftmost digits are truncated, leaving only the right one digit or
     * right two digits. If there is only one digit, it is zero padded to enforce a two digit string
     * result. This function is used by the DatePickerUtilities.doesParsedDateMatchText() function.
     */
    private static String forceNumberStringToTwoDigits(String text) {
        while (text.length() < 2) {
            text = "0" + text;
        }
        if (text.length() > 2) {
            text = text.substring(text.length() - 2, text.length());
        }
        return text;
    }
}

Related

  1. changeLocalDateToFormattedString(LocalDate value)
  2. dateToString(LocalDate date, DateTimeFormatter formatter)
  3. format(final LocalDate source)
  4. formateDate(LocalDate date, String formatStr)
  5. formatLocalDate(LocalDate date)
  6. formatLocalDateJsr310ForJsonPath(LocalDate date)