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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:12:00+00:00 2026-05-26T04:12:00+00:00

I am writing some simple code that tries to deduce whether or not a

  • 0

I am writing some simple code that tries to deduce whether or not a specific String is actually a Java date and, if yes, identify its format (pattern).

Obviously, because there are many possible date formats, establishing which one is applicable for a string requires successive pattern matching, which is really time and CPU-consuming, given that the input string can have other values, too.

So, what I have ended up doing, for a String variable called input, is something like

String datePattern;

if (isLikeDate(input))
{
    datePattern = matchAnyOfThePredefinedDatePatterns(input);
}

where the isLike... method rejects obvious non-date strings and the match... method goes over about 40-50 predefined patterns, trying to construct a valid SimpleDateFormat object. The constructor throws an exception if the input string is not a valid date for the pattern examined each time.

The exception handling slows things down dramatically, but there seems to be no avoiding it. The Apache Commons Date packages exhibit similar performance.

Is there any faster way of implementing this date pattern matching?

  • 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-26T04:12:00+00:00Added an answer on May 26, 2026 at 4:12 am

    Depending on the complexity of the patterns, you might want to match each potential pattern with a regex (or hand-written code) before trying to parse it properly as a date. For example, if the pattern is “yyyyMMddThh:mm:ss” you could check for the length, the position of the T, the position of the colons, and that everything else is a digit before passing it on to the date parsing code.

    This level of pattern matching can be very liberal – it’s only trying to rule out definite infringements of the pattern. The important thing is that it doesn’t reject any values which are actually valid.

    The downside is that for any pattern which does match, you’re doing work twice – but that may well still be easily balanced by significantly reducing the number of exceptions you throw.

    EDIT: Just to clarify, you’re currently testing whether it looks like it could match any of the patterns, and then testing all of them. I’m suggesting that you have a regex for each pattern, and only try parsing against patterns which have already matched the corresponding regex.

    I’d also suggest trying Joda Time – not only is it a generally better API, but its patterns are thread-safe, so you can reuse them. Presumably you’re currently creating new SimpleDateFormat objects each time you have something to parse.

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

Sidebar

Related Questions

I'm writing some code which could really do with some simple compile time metaprogramming.
I'm looking into writing simple graphics code in Android and I've noticed some synchronized()
I am writing a Catalyst web application that presents some data that does not
I am writing a simple bookmarklet that runs on youtube page and fetch some
I'm writing a simple script, that fetches the details of some packages from the
I'm writing code to use UIImagePickerController. Corey previously posted some nice sample code on
I am trying to start writing some simple jQuery plugins and for my first
I'm playing around with writing some simple Spring-based web apps and deploying them to
I'm writing an application which reads large arrays of floats and performs some simple
I'd like to play around with writing some very small, simple web-based games (checkers,

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.