且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

判断字符串是否为有效日期的最快方法

更新时间:2023-11-26 21:31:16

如果您真的关心性能而且日期格式非常简单,那么只需预先计算所有有效的字符串并在内存中散列它们。您上面的格式只有大约800万有效组合,最多2050

If you're really concerned about performance and your date format is really that simple, just pre-compute all the valid strings and hash them in memory. The format you have above only has ~ 8 million valid combinations up to 2050

Slanec编辑 - 参考实现

此实现取决于您的特定日期格式。它可以适应任何特定的日期格式(就像我的第一个答案,但更好一点)。

This implementation depends on your specific dateformat. It could be adapted to any specific dateformat out there (just like my first answer, but a bit better).

它使一组所有日期从1900年到2050年(存储为字符串 - 其中有54787个),然后将给定日期与存储日期进行比较。

It makes a set of all dates from 1900 to 2050 (stored as Strings - there are 54787 of them) and then compares the given dates with those stored.

日期设置已创建,它很快就像地狱一样。与我的第一个解决方案相比,快速微基准测试显示了10倍的改进。

Once the dates set is created, it's fast as hell. A quick microbenchmark showed an improvement by a factor of 10 over my first solution.

private static Set<String> dates = new HashSet<String>();
static {
    for (int year = 1900; year < 2050; year++) {
        for (int month = 1; month <= 12; month++) {
            for (int day = 1; day <= daysInMonth(year, month); day++) {
                StringBuilder date = new StringBuilder();
                date.append(String.format("%04d", year));
                date.append(String.format("%02d", month));
                date.append(String.format("%02d", day));
                dates.add(date.toString());
            }
        }
    }
}

public static boolean isValidDate2(String dateString) {
    return dates.contains(dateString);
}

P.S。它可以修改为使用 Set< Integer> 甚至 Trove TIntHashSet 可以大大减少内存使用量(因此允许使用更大的时间跨度),然后性能下降到低于我的原始解决方案

P.S. It can be modified to use Set<Integer> or even Trove's TIntHashSet which reduces memory usage a lot (and therefore allows to use a much larger timespan), the performance then drops to a level just below my original solution.