简体   繁体   中英

Avoiding a particular check for YYYY-MM--dd format in date

I have below method in which different date patterns have been handled

below is the method in which different date formats have been handled now

now for the particulat format YYYY-MM-dd i don't want it to go for the check where we are prefixing 20 before in code please advise how can i skip that part lets say if the date pattern is YYYY-MM-dd then avoid the logic of prefixing 20 in front of year

below is my code

public java.util.Date extractDate(String dateStr, String dateType) {

        String[] datePatternsOfUk = { "d-M-yy", "d-M-yyyy", "d/M/yy", "d/M/yyyy", "yyyy-MM-dd","dd-MM-yy", "dd-MMM-yy","dd-MMM-yyyy","dd-MM-yyyy",
                "dd/MM/yy","dd/MMM/yy","dd/MMM/yyyy"};
        String[] datePatternsOfUs = { "M-d-yy","MM-dd-yy","M/d/yy","MM/dd/yy", "MM/dd/yy", "MMM-dd-yy",
                "MMM/dd/yy", "MMM-dd-yyyy", "MM-dd-yyyy", "MMM/dd/yyyy",
                "MM/dd/yyyy" };
        java.util.Date date = null;
        String[] datePatterns = datePatternsOfUk;

        if (dateType.equals("US")) {

            datePatterns = datePatternsOfUs;
        } else if (dateType.equals("UK")) {

            datePatterns = datePatternsOfUk;
        }


        ///******code should not go in this check where date pattern is YYYY-MM-dd 
        int p = dateStr.lastIndexOf("/");
        if (p == -1) {
            p = dateStr.lastIndexOf("-");
        }
        String firstSubstring = dateStr.substring(0, p + 1);
        String secondSubstring = dateStr.substring(p + 1);
        if (p != -1 && secondSubstring.length() <= 2) {
            secondSubstring = Integer.toString(2000 + Integer.parseInt(secondSubstring));
            dateStr = firstSubstring + secondSubstring;
        }


        ///****************************************//

        try {
            date = DateUtils.parseDate(dateStr, datePatterns);

        } catch (ParseException ex) {
            logger.error("##$$$$$### Error in invoice inside extractDate method : ##$$$$$$#### "
                    + ErrorUtility.getStackTraceForException(ex));

        }
        return date;
    }

You could avoid trying any inappropriate pattern by checking if the string "looks like" the pattern before parsing with the pattern.

The general way to do this is:

String datePattern = "yyyy-MM-dd"; // for example
String input; 

if (input.matches(datePattern.replaceAll("\\w", "\\d"))) {
    // the input looks like the pattern
    // in this example "dddd-dd-dd" where "d" is any digit
    // so go ahead and try the parse
}

You can enhance this logic to add:

if (input.matches("\\d\\d\\D.*")) {
    // then it only has a two digit year, so add "20" to the front
}
if (!dateStr.equals("YYYY-MM-dd")) {
    // code
}

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