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 7781627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:17:19+00:00 2026-06-01T19:17:19+00:00

When parsing a YYYYMMdd date, e.g. 20120405 for 5th April 2012, what is the

  • 0

When parsing a YYYYMMdd date, e.g. 20120405 for 5th April 2012, what is the fastest method?

int year = Integer.parseInt(dateString.substring(0, 4));
int month = Integer.parseInt(dateString.substring(4, 6));
int day = Integer.parseInt(dateString.substring(6));

vs.

int date = Integer.parseInt(dateString)
year = date / 10000;
month = (date % 10000) / 100; 
day = date % 100;

mod 10000 for month would be because mod 10000 results in MMdd and the result / 100 is MM

In the first example we do 3 String operations and 3 “parse to int”, in the second example we do many things via modulo.

What is faster? Is there an even faster method?

  • 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-06-01T19:17:21+00:00Added an answer on June 1, 2026 at 7:17 pm

    As you see below, the performance of the date processing only is relevant when you look at millions of iterations. Instead, you should choose a solution that is easy to read and maintain.

    Although you could use SimpleDateFormat, it is not reentrant so should be avoided. The best solution is to use the great Joda time classes:

    private static final DateTimeFormatter DATE_FORMATTER = new DateTimeFormatterBuilder()
         .appendYear(4,4).appendMonthOfYear(2).appendDayOfMonth(2).toFormatter();
    ...
    Date date = DATE_FORMATTER.parseDateTime(dateOfBirth).toDate();
    

    If we are talking about your math functions, the first thing to point out is that there were bugs in your math code that I’ve fixed. That’s the problem with doing by hand. That said, the ones that process the string once will be the fastest. A quick test run shows that:

    year = Integer.parseInt(dateString.substring(0, 4));
    month = Integer.parseInt(dateString.substring(4, 6));
    day = Integer.parseInt(dateString.substring(6));
    

    Takes ~800ms while:

    int date = Integer.parseInt(dateString);
    year = date / 10000;
    month = (date % 10000) / 100; 
    day = date % 100;
    total += year + month + day;
    

    Takes ~400ms.

    However … again… you need to take into account that this is after 10 million iterations. This is a perfect example of premature optimization. I’d choose the one that is the most readable and the easiest to maintain. That’s why the Joda time answer is the best.

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

Sidebar

Related Questions

For parsing player commands, I've most often used the split method to split a
im parsing a JSON and cant seem to extract an integer. If i do
When parsing a Jira Custom Field containing a date (e.g. 13/Nov/11) I started with
Parsing a text file in vb.net and need to locate the latitude and longitude
Parsing the string message seems bad. Or was this exception not meant to be
When parsing the JSON, all variables are correct but when converting the epoch to
Im parsing an xml file. My XML handler, my object that holds an Arraylist
After parsing a json file which contains names and ages of people, {People: [
I`m parsing SQL query with C# Regex. I need also to make my pattern
When parsing an xml file in android, I'm doing like this: try { InputStream

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.