是否可以使用字符串解析字符串来捕获两种不同的时间格式?

Is it possible to use a string parse string to capture two different time formats?

我正在使用 Java 8. 如果字符串的形式为 "hh:mm p"(例如“3: 00 am”)或者如果字符串是军用时间格式(例如“15:00”)。我不清楚如何编写表达式来捕获 either/or 场景。现在,我只考虑两种情况之一......

        final DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
        try {
            final java.util.Date startDate = dateFormat.parse(session.getStartTime__c());
            startTime = new Time(startDate.getTime());
        } catch (ParseException e) {
            LOG.error(e.getMessage(), e);
        }   // try

有没有一种方法可以重写上面的代码以一种格式捕获两种情况,或者我是否需要测试字符串然后根据我的测试结果使用不同的格式字符串?

尝试第一个,如果不行,再尝试第二个

    final DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
    final DateFormat dateFormat2 = new SimpleDateFormat(...);
    java.util.Date startDate = null;
    try {
        startDate = dateFormat.parse(session.getStartTime__c());
        startTime = new Time(startDate.getTime());
    } catch (ParseException e) {
       try {
         startDate = dateFormat2.parse(session.getStartTime__c());
       } catch (ParseException e) {
        LOG.error(e.getMessage(), e);
       }
    }   // try