Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6547187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:47:58+00:00 2026-05-25T11:47:58+00:00

What is the quickest way for converting a date which is a string with

  • 0

What is the quickest way for converting a date which is a string with the format “20110913” to “2011-09-13” in Java.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:47:58+00:00Added an answer on May 25, 2026 at 11:47 am

    I did some simple profiling and found some interesting results.

    public static String strcat(String ori){
        return ori.substring(0, 4) + '-' + ori.substring(4, 6) + '-' + ori.substring(6);
    }
    
    public static String sdf(String ori){
        try {
            SimpleDateFormat in = new SimpleDateFormat("yyyyMMdd");
            SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd");
            Date temp = in.parse(ori);
            return out.format(temp);
        } catch (ParseException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    
    public static String sb(String ori){
        return new StringBuilder(10).append(ori.substring(0, 4)).append('-').append(ori.substring(4, 6)).append('-').append(ori.substring(6)).toString();
    }
    
    public static String nio(String ori){
        byte[] temp = ori.getBytes();
        ByteBuffer bb = ByteBuffer.allocate(temp.length + 2);
        bb.put(temp, 0, 4);
        byte hyphen = '-';
        bb.put(hyphen);
        bb.put(temp, 4, 2);
        bb.put(hyphen);
        bb.put(temp, 6, 2);
        return new String(bb.array());
    }
    
    public static String qd(String ori){
        char[] result = new char[10];
        result[4] = result[7] = '-';
    
        char[] temp = ori.toCharArray();
        System.arraycopy(temp, 0, result, 0, 4);
        System.arraycopy(temp, 4, result, 5, 2);
        System.arraycopy(temp, 6, result, 8, 2);
        return new String(result);
    }
    
    public static void main(String[] args) {
        String ori = "20110913";
        int rounds = 10000;
        for (int i = 0; i < rounds; i++) {
            qd(ori);
            nio(ori);
            sb(ori);
            sdf(ori);
            strcat(ori);
        }
    }
    

    With the above few methods, I ran the test three times and the results(averaged) are as follow:-

    sb      15.7ms
    strcat  15.2ms
    qd      27.2ms
    nio     137.6ms
    sdf     582ms
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the quickest way in ColdFusion (or Java) for converting a string as
What's the quickest way to convert a date in one format, say 2008-06-01 to
what's the quickest way to extract a 5 digit number from a string in
How can I convert in the quickest way String[] to Vector<String> ?
What is the quickest way to find out which .net framework linq methods (e.g
Which is the quickest way to view the value of a NSCFString variable in
What is the quickest way to export a very large string (several megabytes) from
What is the quickest way to read a text file into a string variable?
What is the quickest way to get a large amount of data (think golf)
What is the quickest way to come up to speed on OpenGL ES 1.x?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.