英文:
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:50
到 9/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:55
到 10/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<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("can't detect between MM/dd and DD/mm");
return firstPartChangeAmount > secondPartChangeAmount;
}
Output:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论