TL; DR
- Use java.time , not inherited classes.
- Each analysis from
String to LocalDateTime with a DateTimeFormatter takes less than 1,500 nanoseconds each (0.0000015 seconds).
java.time
You are using the nasty old time classes that are now obsolete, being superseded by java.time classes.
Do some micro benchmarking to see how slow / fast parses the date string in java.time.
ISO 8601
The ISO 8601 standard defines reasonable practical formats for the textual representation of date and time values. The java.time classes use these standard default formats when parsing / generating strings.
Use these standard formats instead of inventing your own, as shown in the Question.
DateTimeFormatter
Define a formatting template that matches your inputs.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd HH:mm:ss:SSS" );
We will analyze every input like LocalDateTime , because there is no time zone indicator or offset-from-UTC at your input. Keep in mind that such values do not represent a moment; they are not a point on the timeline. The actual moment requires a zone / offset context.
String inputInitial = "2012-05-02 12:08:06:950" ; LocalDateTime ldtInitial = LocalDateTime.parse( inputInitial , f );
Let me 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 ); }
Wiring 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 for the console.
System.out.println( "Parsing " + count + " strings to LocalDateTime took: " + d + ". About " + nanosPerParse + " nanos each.");
Parsing 1,000,000 lines in LocalDateTime took: PT1.320778647S. About 1320 sediment each.
Too slow?
Thus, on a MacBook Pro with a quad-core Intel i7 processor, it takes about a second and a half to parse a million of these inputs. In my test runs, each parsing takes from 1000 to 1500 nanoseconds each.
In my opinion, this is not a performance issue.
About java.time
The java.time framework is built into Java 8 and later. These classes supersede the nasty old legacy time classes such as java.util.Date , Calendar and SimpleDateFormat .
The Joda-Time project, now in maintenance mode , is advised to switch to the java.time classes.
To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations. JSR 310 specification .
You can exchange java.time objects directly with your database. Use a JDBC driver that conforms to JDBC 4.2 or later. No strings needed, no java.sql.* Classes needed.
Where to get java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and more .