如何最佳确定 Java 中的日期格式是 MM/dd 还是 dd/MM。

huangapple go评论86阅读模式
英文:

How to best determine whether the date format is MM/dd or DD/mm in java

问题

我有一系列的文件,需要使用Java进行分析。日期字段从文件中以 String 格式读取,我需要将其解析为 LocalDateTime 格式。问题是现在不知道日期的第一个部分是月份还是日期。

它们中的格式可以是任何格式,但最有可能是以下格式。

"yyyy-MM-dd HH:mm:ss","dd/MM/yyyy HH:mm","MM/dd/yyyy HH:mm","yyyy-MM-dd HH:mm",
        "MM/dd/yy HH:mm"

例如:

9/8/2020 23:50
9/8/2020 23:55
9/9/2020 00:00

在上述情况下,可以通过观察日期从 9/8/2020 23:509/9/2020 00:00 的变化来猜测日期字段。这意味着日期从8号变为9号,因此格式为 MM/dd/yyyy HH:mm

9/8/2020 23:00
9/8/2020 23:50
10/8/2020 00:00

在上述情况下,可以通过观察日期从 9/8/2020 23:5510/9/2020 00:00 的变化来猜测日期字段。这意味着日期从9号变为10号,因此格式为 dd/MM/yyyy HH:mm

文件的格式还可以是 2020-09-08 23:00:00。我唯一知道的是日期会在系列中发生变化,而月份很少会变化。

解决这个问题的最佳方法是什么?

英文:

I have list of files coming and I need to analyze them using java. The date field is read from files as String and I need to parse it in to LocalDatTime. The problem is it is now known that whether the first part of date is month or day.

The format in them can be anything but most probably in the following formats.

"yyyy-MM-dd HH:mm:ss", "dd/MM/yyyy HH:mm","MM/dd/yyyy HH:mm", "yyyy-MM-dd HH:mm",
        "MM/dd/yy HH:mm"

e.g.

9/8/2020 23:50
9/8/2020 23:55
9/9/2020 00:00

In the above case date field can be guessed from when the date changes from 9/8/2020 23:50
9/9/2020 00:00 . This means the date changed from 8th to 9th and hence the format is MM/dd/yyyy HH:mm

9/8/2020 23:00
9/8/2020 23:50
10/8/2020 00:00

In the above case date field can be guessed from when the date changes from 9/8/2020 23:55
10/9/2020 00:00 . This means the date changed from 9th to 10th and hence the format is dd/MM/yyyy HH:mm

The file can also be 2020-09-08 23:00:00. The only thing I know is that the date will change in the series while the month will seldom change.

What is the best way to cater this issue.

答案1

得分: 1

以下是翻译好的代码部分:

// 判断是否为日/月/年格式或月/日格式
public static boolean isDayMonthYearFormat(List<String> sortedDates) {
    int firstPartChangeAmount = 0;
    int secondPartChangeAmount = 0;
    int prvOne = -1;
    int prvTwo = -1;
    boolean count = false;

    for (String date : sortedDates) {
        String[] parts = date.split("[\\/\\s+:]");
        int one = Integer.parseInt(parts[0]);
        int two = Integer.parseInt(parts[1]);

        if (count) {
            firstPartChangeAmount += prvOne < one ? 1 : 0;
            secondPartChangeAmount += prvTwo < two ? 1 : 0;
        }

        count = true;
        prvOne = one;
        prvTwo = two;
    }

    if (firstPartChangeAmount == secondPartChangeAmount)
        throw new RuntimeException("无法区分 MM/dd 和 DD/mm 格式");

    return firstPartChangeAmount > secondPartChangeAmount;
}

// 输出结果
System.out.println(isDayMonthYearFormat(Arrays.asList("9/8/2020 23:50", "9/8/2020 23:55", "9/9/2020 00:00")));  // false
System.out.println(isDayMonthYearFormat(Arrays.asList("9/8/2020 23:00", "9/8/2020 23:50", "10/8/2020 00:00"))); // true
英文:

One of the sulution could be just counting of changes of first and second part and compare result. This is not extra efficient, but quite simple:

// true - day/month/year
// false - month/day
public static boolean isDayMonthYearFormat(List&lt;String&gt; sortedDates) {
    int firstPartChangeAmount = 0;
    int secondPartChangeAmount = 0;
    int prvOne = -1;
    int prvTwo = -1;
    boolean count = false;

    for (String date : sortedDates) {
        String[] parts = date.split(&quot;[\\/\\s+:]&quot;);
        int one = Integer.parseInt(parts[0]);
        int two = Integer.parseInt(parts[1]);

        if (count) {
            firstPartChangeAmount += prvOne &lt; one ? 1 : 0;
            secondPartChangeAmount += prvTwo &lt; two ? 1 : 0;
        }

        count = true;
        prvOne = one;
        prvTwo = two;
    }

    if (firstPartChangeAmount == secondPartChangeAmount)
        throw new RuntimeException(&quot;can&#39;t detect between MM/dd and DD/mm&quot;);

    return firstPartChangeAmount &gt; secondPartChangeAmount;
}

Output:

System.out.println(isDayMonthYearFormat(Arrays.asList(&quot;9/8/2020 23:50&quot;, &quot;9/8/2020 23:55&quot;, &quot;9/9/2020 00:00&quot;)));  // false
System.out.println(isDayMonthYearFormat(Arrays.asList(&quot;9/8/2020 23:00&quot;, &quot;9/8/2020 23:50&quot;, &quot;10/8/2020 00:00&quot;))); // true

huangapple
  • 本文由 发表于 2020年10月23日 23:54:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64503362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定