How to parse dates in multiple formats using SimpleDateFormat

前端 未结 12 1481
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 08:41

I am trying to parse some dates that are coming out of a document. It would appear users have entered these dates in a similar but not exact format.

here are the for

12条回答
  •  隐瞒了意图╮
    2020-11-22 09:30

    If working in Java 1.8 you can leverage the DateTimeFormatterBuilder

    public static boolean isTimeStampValid(String inputString)
    {
        DateTimeFormatterBuilder dateTimeFormatterBuilder = new DateTimeFormatterBuilder()
                .append(DateTimeFormatter.ofPattern("" + "[yyyy-MM-dd'T'HH:mm:ss.SSSZ]" + "[yyyy-MM-dd]"));
    
        DateTimeFormatter dateTimeFormatter = dateTimeFormatterBuilder.toFormatter();
    
        try {
            dateTimeFormatter.parse(inputString);
            return true;
        } catch (DateTimeParseException e) {
            return false;
        }
    }
    

    See post: Java 8 Date equivalent to Joda's DateTimeFormatterBuilder with multiple parser formats?

提交回复
热议问题