Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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</p> <hr> <p><strong>EDIT by Slanec</strong> - reference implementation</p> <p>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).</p> <p>It makes a set of all <code>dates</code> from 1900 to 2050 (stored as Strings - there are 54787 of them) and then compares the given dates with those stored.</p> <p>Once the <code>dates</code> set is created, it's fast as hell. A quick microbenchmark showed an improvement by a factor of 10 over my first solution.</p> <pre><code>private static Set&lt;String&gt; dates = new HashSet&lt;String&gt;(); static { for (int year = 1900; year &lt; 2050; year++) { for (int month = 1; month &lt;= 12; month++) { for (int day = 1; day &lt;= 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); } </code></pre> <p>P.S. It can be modified to use <code>Set&lt;Integer&gt;</code> or even <a href="http://trove.starlight-systems.com/" rel="nofollow noreferrer">Trove</a>'s <code>TIntHashSet</code> which reduces memory usage a lot (and therefore allows to use a much larger timespan), the performance then drops to a level just below <a href="https://stackoverflow.com/a/11480602/1273080">my original solution</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload