Parsing a date in Java?

前端 未结 7 1572
清酒与你
清酒与你 2020-12-04 03:15

I have dates in text format of the form dd-mmm-yy or d-mmm-y, where months are abbreviations in letters (for example, 4-Nov-09 or 12-Dec-05, etc...) I would like to parse it

7条回答
  •  一生所求
    2020-12-04 03:40

    java.time

    This is the modern answer. The DateFormat class mentioned in the question and its SimpleDateFormat subclass used in most of the old answers are notoriously troublesome and long outdated. Don’t use any of those. Don’t use Date either, it’s poorly designed and long outdated too.

    Since you have got a two-digit year (09 and 05 in your examples), you need to decide on a century. In the first round I am assuming that the year is in the range 2000 through 2099.

    First we need a formatter:

    static DateTimeFormatter dateFormatter
            = DateTimeFormatter.ofPattern("d-MMM-uu", Locale.ENGLISH);
    

    DateTimeFormatter is thread safe, so we’re happy with one instance for all to use. @shoover is correct that we should provide a locale. Use the formatter like this:

        String text = "4-Nov-09";
        LocalDate date = LocalDate.parse(text, dateFormatter);
        System.out.println("Date: " + date);
    

    Output is:

    Date: 2009-11-04

    Validate

    For validation, in particular of the correct interpretation of the two-digit year, I recommend that you add a range check. For example, to validate that the parsed date is not more than 10 years into the past or future:

        LocalDate today = LocalDate.now(ZoneId.systemDefault());
        if (date.isBefore(today.minusYears(10))
                || date.isAfter(today.plusYears(10))) {
            System.err.println("This date can’t be right");
        }
    

    For many purposes you will know in advance that the date is either in the past or in the future and can narrow down the valid interval even further than in my example.

    Other centuries

    If the years can go outside the 20xx range, we control the interpretation of the year through a DateTimeFormatterBuilder and its appendValueReduced method. Refer to one of the related answers in the list of links below.

    Links

    • Oracle tutorial: Date Time explaining how to use java.time.
    • Answer: How to change the base date for parsing two letter years with Java 8 DateTimeFormatter?
    • Answer: How to convert two digit year to full year using Java 8 time API

提交回复
热议问题