What is the quickest way for converting a date which is a string with the format “20110913” to “2011-09-13” in Java.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I did some simple profiling and found some interesting results.
With the above few methods, I ran the test three times and the results(averaged) are as follow:-
The test is ran using JDK 7. Do take note that this is not an extensive profiling as there are a lot of optimization that can be done (e.g. caching the SimpleDateFormat, StringBuilder). Also, this test is not multithreaded. So, do profile your own program!
p.s. the strcat is faster than sb is not what I expected. I guess the compiler optimization plays a big role here.