18

I am having string like

"11-04-2015 22:01:13:053" or "32476347656435"

How can I check if string is Date?
Checking String if it is numeric using regex

String regex = "[0-9]+";
Tushar
  • 85,780
  • 21
  • 159
  • 179
Neeti
  • 377
  • 1
  • 4
  • 16

5 Answers5

35

Other person are also correct

This is your answer

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class date {
    public static boolean isValidDate(String inDate) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss:ms");
        dateFormat.setLenient(false);
        try {
            dateFormat.parse(inDate.trim());
        } catch (ParseException pe) {
            return false;
        }
        return true;
    }

    public static void main(String[] args) {

        System.out.println(isValidDate("20-01-2014"));
        System.out.println(isValidDate("11-04-2015 22:01:33:023"));

        System.out.println(isValidDate("32476347656435"));
    }
}
Mariano
  • 6,423
  • 4
  • 31
  • 47
amit prasad
  • 574
  • 3
  • 15
  • There is multiple date formats so yo have to check a variety of them e.g. `yyyy/MM/dd` – Peter Lawrey Nov 28 '15 at 08:03
  • Never use exceptions for data flowing. They are hurts performance. – Eldar Agalarov Nov 03 '20 at 09:21
  • There are bugs in the code. It rejects the fully valid `11-04-2015 22:01:33:999`. And it accepts all of the following three, which may or may not be desired (1) `11-04-2015 22:01:33:023 and some nonsense here` (2) `1-4-5 2:1:3:23` (3) `011-004-22001155 22:001:33:500023`. The bugs are mainly due to the confusing and poor design of `SimpleDateFormat`. You should avoid using that class. – Ole V.V. Jul 24 '23 at 11:02
5

java.time

It’s about time someone provides the modern answer. The SimpleDateFormat class mentioned in a couple of the other answers is notoriously troublesome and fortunately now long outdated. Instead the modern solution uses java.time, the modern Java date and time API.

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-uuuu HH:mm:ss:SSS");

    String stringToTest = "11-04-2015 22:01:13:053";

    try {
        LocalDateTime dateTime = LocalDateTime.parse(stringToTest, formatter);
        System.out.println("The string is a date and time: " + dateTime);
    } catch (DateTimeParseException dtpe) {
        System.out.println("The string is not a date and time: " + dtpe.getMessage());
    }

Output from this snippet is:

The string is a date and time: 2015-04-11T22:01:13.053

Suppose that instead the string was defined as:

    String stringToTest = "32476347656435";

Now the output is:

The string is not a date and time: Text '32476347656435' could not be parsed at index 2

Link: Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
1

There are two possible solutions:

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
0

The best solution is to actually try to parse it to a date with DateTime.TryParse().

String d = "11-04-2015 22:01:13:053";

DateTime dt = new DateTime();

if (DateTime.TryParse(d, out dt)) { 
    /// yes, it's a date, do something here... 
} else {
    // no, it's not a date, do something else here... 
}
Matt Ke
  • 3,599
  • 12
  • 30
  • 49
Sean Munson
  • 343
  • 2
  • 9
  • This solution is only useful to check if a string with a specific format can be interpretted as a valid date. Dates can be written in so many ways, so a more intelligent match routine is probably what is called for. Further more, some strings that represent dates could have multiple interpretations, for example "03/03/08". Is it March 3rd 2008 - or is it 8th of March 2003? – Mads Boyd-Madsen Jan 30 '19 at 22:31
  • 9
    I believe that `DateTime.TryParse` is a .NET function not accessible from Java. And I believe a Java solution was asked for. – Ole V.V. Nov 07 '19 at 19:07
0

You can use Apache Commons Validator. It provides validation framework for validating date, time, numbers, currency, IP address, email, and URL.

Maven dependency:

<dependency>
    <groupId>commons-validator</groupId>
    <artifactId>commons-validator</artifactId>
    <version>1.6</version>
</dependency>

Example:

assertTrue(GenericValidator.isDate("2019-02-28", "yyyy-MM-dd", true))
Matt Ke
  • 3,599
  • 12
  • 30
  • 49