【问题标题】:Date format matching in Groovy [duplicate]Groovy中的日期格式匹配[重复]
【发布时间】:2019-01-24 02:48:39
【问题描述】:

嗨,我有一个看起来像这样的字符串 - Wednesday 16 January 2019。 如果此日期与EEEE DD MMMM YYYY 格式匹配,我想检查一下。 有没有办法做到这一点?

有没有我可以使用的内置函数,或者正则表达式是我唯一的选择?

干杯!

【问题讨论】:

    标签: java regex date datetime groovy


    【解决方案1】:

    你可以尝试用模式EEEE dd MMMM yyyy解析它,如果抛出异常,那么它不在这个模式中,在java中它看起来像:

    public boolean isInDesiredFormat(String input) {
        try {
            DateTimeFormatter format = DateTimeFormatter.ofPattern("EEEE dd MMMM yyyy", Locale.ENGLISH);
            LocalDate.parse(input, format);
            return true;
        } catch (Exception ignore) {
            ignore.printStackTrace();
            return false;
        }
    }
    

    【讨论】:

    • this answer 中所述,您可能需要申请ResolverStyle.STRICT 进行更严格的验证。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    • 2013-05-06
    • 2016-01-20
    • 2015-02-24
    相关资源
    最近更新 更多