Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As you see below, the performance of the date processing only is relevant when you look at <em>millions</em> of iterations. Instead, you should choose a solution that is easy to read and maintain.</p> <p>Although you could use <code>SimpleDateFormat</code>, it is not reentrant so should be avoided. The best solution is to use the great Joda time classes:</p> <pre><code>private static final DateTimeFormatter DATE_FORMATTER = new DateTimeFormatterBuilder() .appendYear(4,4).appendMonthOfYear(2).appendDayOfMonth(2).toFormatter(); ... Date date = DATE_FORMATTER.parseDateTime(dateOfBirth).toDate(); </code></pre> <hr> <p>If we are talking about your math functions, the first thing to point out is that there were bugs in your math code that I've fixed. <em>That's</em> the problem with doing by hand. That said, the ones that process the string once will be the fastest. A quick test run shows that:</p> <pre><code>year = Integer.parseInt(dateString.substring(0, 4)); month = Integer.parseInt(dateString.substring(4, 6)); day = Integer.parseInt(dateString.substring(6)); </code></pre> <p>Takes ~800ms while:</p> <pre><code>int date = Integer.parseInt(dateString); year = date / 10000; month = (date % 10000) / 100; day = date % 100; total += year + month + day; </code></pre> <p>Takes ~400ms.</p> <p><strong>However ... again...</strong> you need to take into account that this is after <strong>10 million</strong> iterations. This is a perfect example of premature optimization. I'd choose the one that is the most readable and the easiest to maintain. That's why the Joda time answer is the best. </p>
 

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