【问题标题】:Parsing date string with different dateformats解析具有不同日期格式的日期字符串
【发布时间】:2016-03-15 11:03:04
【问题描述】:

我正在尝试解析日期字符串,我的问题是这些字符串可以有不同的日期格式,这取决于他们谈论的是今天、明天还是另一天。

  • 如果他们谈论今天的活动,格式是这样的:20:45
  • 如果他们谈论明天的活动,格式是:明天 20:45
  • 如果他们谈论另一天,格式是:2016 年 5 月 10 日

所以我想知道我是否可以用相同的 DateFormat 解析它们三个,如果不是最好的方法。

DateFormat format = new SimpleDateFormat("EEEE d ' de' MMMM ' de' yyyy", locale);

【问题讨论】:

  • 您使用哪个 Java 版本?
  • 不,您不能使用相同的 DateFormat 实例解析它们,因为这需要将值解析为具有给定格式。如果您确定这些是您可以获得的唯一格式,您可以首先检查字符串的长度 == 5,如果它是今天,然后检查字符串是否包含“明天”,如果它们都不正确,您可以格式化它使用日期格式。

标签: java parsing date-format


【解决方案1】:

您不能使用相同的 SimpleDateFormat 来解析所有类型,也不是一个好习惯,不可读且更复杂,不添加任何特殊值,我会尝试这样的事情:

private static final SimpleDateFormat formatHHMM = new SimpleDateFormat("hh:mm");
private static final SimpleDateFormat formatOther = new SimpleDateFormat("MMM dd yyyy");

private static String convertDate(Date curDate) {
    if (isToday(curDate)) {
        return formatHHMM.format(curDate);
    }
    else if (isTomorrow(curDate)) {
        return "Tomorrow " + formatHHMM.format(curDate);
    }
    return formatOther.format(curDate);
}

private static boolean isToday(Date curDate) {
    Date today = Calendar.getInstance().getTime();
    return today.equals(curDate);
}

private static boolean isTomorrow(Date curDate) {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, 1);
    Date tomorrow = calendar.getTime();
    return tomorrow.equals(curDate);
}
//Check the code with this
public static void main( String[] args )
{
    Date curDate = new Date();
    System.out.println( convertDate(curDate));

    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, 1);
    Date tomorrow = calendar.getTime();
    System.out.println( convertDate(tomorrow));

    calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, 15);
    Date other = calendar.getTime();
    System.out.println( convertDate(other));
}

【讨论】:

  • 我认为 OP 想要一个解决方案来解析不同的日期格式,而不是 format
【解决方案2】:

我认为不可能使用相同的格式化程序解析这 3 种格式。虽然可能只用一个格式化程序来解析前两个,但意义的不同(今天或明天)将意味着不同的逻辑,这是用一个格式化程序无法获得的。

我建议你分别处理每个案例,一个接一个地尝试:

    try {
        return LocalDate
                .now()
                .atTime(LocalTime.parse(date));
    } catch (DateTimeParseException e) {
        try {
            return LocalDate
                    .now()
                    .plusDays(1)
                    .atTime(LocalTime.parse(date, DateTimeFormatter.ofPattern("'tomorrow' HH:mm")));
        } catch (DateTimeParseException e1) {
            return LocalDate
                    .parse(date, DateTimeFormatter.ofPattern("MMMM dd yyyy", Locale.ENGLISH))
                    .atStartOfDay();
        }
    }

不过,我不确定控制流的清晰度。如果输入格式的数量增加,可能需要重构为更清晰的内容。

【讨论】:

  • 好吧,我不喜欢通过异常捕获来控制流,但这不是你的错(这里是由 API 限制强制执行的)。无论如何,性能会受到影响,这是肯定的。
  • 我不认为在异常流中添加逻辑是一个明确的解决方案。
【解决方案3】:
if (DateFormat.charAt(0).isDigit() && DateFormat.charAt(1).isDigit() && DateFormat.charAt(2).isLetterOrDigit()==false  .... {
new String SimpleDateFormat= '15.03.2016';
else {

if (DateFormat.charAt(0).isLetter('t') &&  DateFormat.charAt(1).isLetter('o') &&   and so on 
 { SimpleDateFormat='16.03.2016'

您可以逐个更改条件以匹配所有类型的数据格式

【讨论】:

  • 请至少向 OP 提供可编译的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多