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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:14:10+00:00 2026-06-07T14:14:10+00:00

I am supporting a common library at work that performs many checks of a

  • 0

I am supporting a common library at work that performs many checks of a given string to see if it is a valid date. The Java API, commons-lang library, and JodaTime all have methods which can parse a string and turn it in to a date to let you know if it is actually a valid date or not, but I was hoping that there would be a way of doing the validation without actually creating a date object (or DateTime as is the case with the JodaTime library). For example here is a simple piece of example code:

public boolean isValidDate(String dateString) {
    SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
    try {
        df.parse(dateString);
        return true;
    } catch (ParseException e) {
        return false;
    }
}

This just seems wasteful to me, we are throwing away the resulting object. From my benchmarks about 5% of our time in this common library is spent validating dates. I’m hoping I’m just missing an obvious API. Any suggestions would be great!

UPDATE

Assume that we can always use the same date format at all times (likely yyyyMMdd). I did think about using a regex as well, but then it would need to be aware of the number of days in each month, leap years, etc…


Results

Parsed a date 10 million times

Using Java's SimpleDateFormat: ~32 seconds 
Using commons-lang DateUtils.parseDate: ~32 seconds
Using JodaTime's DateTimeFormatter: ~3.5 seconds 
Using the pure code/math solution by Slanec: ~0.8 seconds 
Using precomputed results by Slanec and dfb (minus filling cache): ~0.2 seconds

There were some very creative answers, I appreciate it! I guess now I just need to decide how much flexibility I need what I want the code to look like. I’m going to say that dfb’s answer is correct because it was purely the fastest which was my original questions. Thanks!

  • 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-07T14:14:13+00:00Added an answer on June 7, 2026 at 2:14 pm

    If you’re really concerned about performance and your date format is really that simple, just pre-compute all the valid strings and hash them in memory. The format you have above only has ~ 8 million valid combinations up to 2050


    EDIT by Slanec – reference implementation

    This implementation depends on your specific dateformat. It could be adapted to any specific dateformat out there (just like my first answer, but a bit better).

    It makes a set of all dates from 1900 to 2050 (stored as Strings – there are 54787 of them) and then compares the given dates with those stored.

    Once the dates set is created, it’s fast as hell. A quick microbenchmark showed an improvement by a factor of 10 over my first solution.

    private static Set<String> dates = new HashSet<String>();
    static {
        for (int year = 1900; year < 2050; year++) {
            for (int month = 1; month <= 12; month++) {
                for (int day = 1; day <= daysInMonth(year, month); day++) {
                    StringBuilder date = new StringBuilder();
                    date.append(String.format("%04d", year));
                    date.append(String.format("%02d", month));
                    date.append(String.format("%02d", day));
                    dates.add(date.toString());
                }
            }
        }
    }
    
    public static boolean isValidDate2(String dateString) {
        return dates.contains(dateString);
    }
    

    P.S. It can be modified to use Set<Integer> or even Trove‘s TIntHashSet which reduces memory usage a lot (and therefore allows to use a much larger timespan), the performance then drops to a level just below my original solution.

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

Sidebar

Related Questions

My company has a common code library which consists of many class libary projects
Apple deprecated Java and will stop supporting it in the future. Assuming that Oracle
We're supporting legacy code that is dropping a cookie and setting the expiration to
Dreamweaver CS5 is supporting SVN up to version 1.6.6, meaning that all later svn
We have a Java application and a supporting Ant build.xml file, and the team
A common problem I find when dealing with non-technical users when supporting technical issues
I'm supporting a multi-tenant system that allows users to define custom forms. The data
As we all know supporting IE6 with its many well documented quirks is painful
My application is supporting 3 type of density ldpi (120),mdpi(160),hdpi(320) and for that we
I am looking for a c# library that is open source but licensed to

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.