tl;dr
The back-port of the java.time classes takes under a microsecond to generate a String
from a LocalDate
in your desired pattern.
String output = myLocalDate.toString() ; // Takes less than a microsecond.
Using this library, I would not worry about date-time strings being a bottleneck.
ThreeTen-Backport
The modern approach uses the java.time classes that supplanted the terrible old date-time classes such as Date
& Calendar
. For Java 6 & 7, most of that functionality is back-ported in the ThreeTen-Backport project, using nearly identical API. Add the library and import: import org.threeten.bp.*;
Your example format of YYYY-MM-DD is the default used by the LocalDate
class when parsing/generating text.
Example code.
Set up a list of LocalDate
objects.
long years = 1000;
LocalDate today = LocalDate.now();
LocalDate lastDate = today.plusYears( years );
int initialCapacity = ( int ) ( ( years + 1 ) * 366 );
List < LocalDate > dates = new ArrayList <>( initialCapacity );
LocalDate localDate = today;
System.out.println( "From: " + today + " to: " + lastDate );
while ( localDate.isBefore( lastDate ) ) {
dates.add( localDate );
// Setup next loop.
localDate = localDate.plusDays( 1 );
}
Run the test.
long start = System.nanoTime();
for ( LocalDate date : dates ) {
String output = date.toString(); // Generate text in standard ISO 8601 format.
}
long stop = System.nanoTime();
long elapsed = ( stop - start );
long nanosEach = elapsed / dates.size();
System.out.println( "nanosEach: " + nanosEach );
Results: Under a microsecond each
When running on a MacBook Pro (Retina, 15-inch, Late 2013), 2.3 GHz Intel Core i7, 16 GB 1600 MHz DDR3, in IntelliJ 2018.3, using Java 10.0.2 from the OpenJDK-based Zulu JVM from Azul Systems…
When running a batch of 100 years, I get about 650 nanoseconds each. That is about two/thirds of a microsecond.
When running a batch of 1,000 years, I get about 260 nanoseconds each. That is about a quarter of a microsecond.
I doubt processing date strings with this library will prove to be a bottleneck in your app’s performance.
Thread-safety
The java.time classes are designed to be inherently thread-safe, including the use of immutable objects.
You can cache a single DateTimeFormatter
object, and use it repeatedly, even across threads.
Your desired format, defined by the ISO 8601 standard, is pre-defined as a constant in both java.time and in the ThreeTen-Backport library: DateTimeFormatter .ISO_LOCAL_DATE
.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd" ) ; // Or just use the pre-defined constant for that particular pattern, `DateTimeFormatter .ISO_LOCAL_DATE`, also used by default in `LocalDate::toString`.
…
String output = localDate.format( f ) ;
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?