大廈Martin Ackermann's answer:
首先,我簡單的代碼位。
public static Map<String, Set<LocalDate>> createDateMapping(LocalDate min, LocalDate max) throws ParseException {
DateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd");
DateTimeFormatter wrongFormat = DateTimeFormatter.ofPattern("dd-MM-yyyy");
final Map<String, Set<LocalDate>> inputMappings = new LinkedHashMap<>();
for (LocalDate date = min; !date.isAfter(max); date = date.plusDays(1)) {
final String incorrectlyFormattedDate = date.format(wrongFormat);
final String key = targetFormat.format(targetFormat.parse(incorrectlyFormattedDate));
if (!inputMappings.containsKey(key)) {
inputMappings.put(key, new TreeSet<>());
}
inputMappings.get(key).add(date);
}
return inputMappings;
}
輕鬆修復無效日期取決於有效日期的範圍。
例如,如果max=2016-12-31
那麼下表顯示了獨特的日期是可以解決的/含糊取決於min
min fixable ambiguous
-----------------------------
1990-01-01 9862 0
1980-01-01 8827 2344
1970-01-01 5331 5918
1960-01-01 1832 9494
1950-01-01 408 10950
1940-01-01 314 11054
1930-01-01 218 11160
1920-01-01 165 11223
1910-01-01 135 11263
1900-01-01 105 11303
爲無效日期不明確的比賽在大約30年爲間隔發生,所以如果實際的日期落在一個數量30年的時間,那麼你很幸運
LocalDate max = LocalDate.of(2016, Month.DECEMBER, 31);
LocalDate min = max.minusYears(30);
Map<String, Set<LocalDate>> invalidDateMapping = createDateMapping(min, max);
long reversibleCount = invalidDateMapping.entrySet().stream().filter(e -> e.getValue().size() == 1).count(); // 10859
long ambiguousCount = invalidDateMapping.size() - reversibleCount; // 50
順便說一句,麻煩的舊日期時間類,如['java.util.Date'](https://docs.oracle.com/javase/ 9/docs/api/java/util/Date.html),['java.util.Calendar'](https://docs.oracle.com/javase/9/docs/api/java/util/Calenda r.html)和'java.text.SimpleDateFormat'現在是[legacy](https://en.wikipedia.org/wiki/Legacy_system),由[* java.time *]代替(https:// docs .oracle.com/javase/9/docs/api/java/time/package-summary.html)內置於Java 8和Java 9的類。請參閱[* Oracle *教程](https://docs.oracle.com /javase/tutorial/datetime/TOC.html)。 –