Recognise an arbitrary date string

后端 未结 14 904
抹茶落季
抹茶落季 2020-12-01 16:18

I need to be able to recognise date strings. It doesn\'t matter if I can not distinguish between month and date (e.g. 12/12/10), I just need to classify the string as being

14条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 16:23

    You can loop all available date formats in Java:

    for (Locale locale : DateFormat.getAvailableLocales()) {
        for (int style =  DateFormat.FULL; style <= DateFormat.SHORT; style ++) {
            DateFormat df = DateFormat.getDateInstance(style, locale);
            try {
                    df.parse(dateString);
                    // either return "true", or return the Date obtained Date object
            } catch (ParseException ex) {
                continue; // unperasable, try the next one
            }
        }
    }
    

    This however won't account for any custom date formats.

提交回复
热议问题