tl;dr
- Use java.time rather than legacy classes.
- Each parse of
String
to LocalDateTime
with DateTimeFormatter
takes less than 1,500 nanoseconds each (0.0000015 seconds).
java.time
You are using troublesome old date-time classes that are now legacy, supplanted by the java.time classes.
Let's do a bit of micro-benchmarking to see just how slow/fast is parsing a date-time string in java.time.
ISO 8601
The ISO 8601 standard defines sensible practical formats for textually representing date-time values. The java.time classes use these standard formats by default when parsing/generating strings.
Use these standard formats instead of inventing your own, as seen in the Question.
DateTimeFormatter
Define a formatting pattern to match your inputs.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd HH:mm:ss:SSS" );
We will parse each such input as a LocalDateTime
because your input lacks an indicator of time zone or offset-from-UTC. Keep in mind that such values do not represent a moment, are not a point on the timeline. To be an actual moment requires the context of a zone/offset.
String inputInitial = "2012-05-02 12:08:06:950" ;
LocalDateTime ldtInitial = LocalDateTime.parse( inputInitial , f );
Let's make a bunch of such inputs.
int count = 1_000_000;
List < String > inputs = new ArrayList <>( count );
for ( int i = 0 ; i < count ; i++ )
{
String s = ldtInitial.plusSeconds( i ).format( f );
inputs.add( s );
}
Test harness.
long start = System.nanoTime();
for ( String input : inputs )
{
LocalDateTime ldt = LocalDateTime.parse( input , f );
}
long stop = System.nanoTime();
long elapsed = ( stop - start );
long nanosPerParse = (elapsed / count ) ;
Duration d = Duration.ofNanos( elapsed );
Dump to console.
System.out.println( "Parsing " + count + " strings to LocalDateTime took: " + d + ". About " + nanosPerParse + " nanos each.");
Parsing 1000000 strings to LocalDateTime took: PT1.320778647S. About 1320 nanos each.
Too slow?
So it takes about a second and a half to parse a million such inputs, on a MacBook Pro laptop with quad-core Intel i7 CPU. In my test runs, each parse takes about 1,000 to 1,500 nanoseconds each.
To my mind, that is not a performance problem.
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?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
Date(long date)
constructor. – Pavyerjava.util.Date
,java.util.Calendar
, andjava.text.SimpleDateFormat
are now legacy, supplanted by the java.time classes built into Java 8 & Java 9. See Tutorial by Oracle. – Anitaanitra