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

  • Home
  • SEARCH
  • 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 6769937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:15:10+00:00 2026-05-26T15:15:10+00:00

I want to convert a string into a date, this is simple. But what

  • 0

I want to convert a string into a date, this is simple. But what I’d like to do it without knowing the date format.

Here is a situation: say I have 100 dates and all are in the same format but I’d like to write a Java program to find out this format for me. The result of this program should give me a list of all the possible formats.

For example:

 06-06-2006
 06-06-2009

 ...

 06-13-2001 <- 99th record 

the result of this will give me date format can be mm-dd-yyyy

If the 99th record also was 06-06-2006 the result should be mm-dd-yyyy and dd-mm-yyyy.

Can someone please help me with an example?

  • 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-26T15:15:10+00:00Added an answer on May 26, 2026 at 3:15 pm

    Seems sensible to create a set of formats you know about (DATE_FORMATS) and then test each line to see which formats understand every line. You should end up with a set of possibilities.

    public class DateFormatDetector {
    
        private static final Set<String> DATE_FORMATS = new HashSet<String>();
        static {
            DATE_FORMATS.add("yyyy-MM-dd");
            DATE_FORMATS.add("dd-MM-yyyy");
            DATE_FORMATS.add("MM-dd-yyyy");
        }
    
        public static Set<String> getPossibleDateFormats(List<String> dates) {
            Set<SimpleDateFormat> candidates = new HashSet<SimpleDateFormat>();
            for (String df : DATE_FORMATS) {
                SimpleDateFormat candidate = new SimpleDateFormat(df);
                candidate.setLenient(false);
                candidates.add(candidate);
            }
    
            for (String date : dates) {
                Iterator<SimpleDateFormat> it = candidates.iterator();
                while (it.hasNext()) {
                    SimpleDateFormat candidate = it.next();
                    try {
                        // try to parse the string as a date
                        candidate.parse(date);
                    }
                    catch (ParseException e) {
                        // failed to parse, so this format is not suitable
                        it.remove();
                    }
                }
            }
    
            Set<String> results = new HashSet<String>();
            for (SimpleDateFormat candidate : candidates)
                results.add(candidate.toPattern());
    
            return results;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert a date string like 19.11.2009 14:00 into the age of
I want to convert the following string into Date type this way Date dateToFormat
I want to convert a string like this: '10/15/2008 10:06:32 PM' into the equivalent
i want to convert this string into DateTime. Tue Aug 19 15:05:05 +0000 2008
I want to convert all texts in a string into html entities but preserving
I want to convert string Date into Timestamp in java. The following coding i
I want convert string value into date in my project because I am fetching
I was trying to format a string into date. For this I have written
I am trying to convert a string into date time format. DateTime.Parse(tempfrmBankDetails.dgvBankDetails.SelectedRows[0].Cells[PaymentDate].Value.ToString(),null); This is
I want to convert a string into a double and after doing some math

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.