Languages
[Edit]
EN

Java - how to check if String is valid date?

13 points
Created by:
Marcin
2462

1. Overview

In this post we can find code which can validate if String contains valid Date based on defined pattern.

2. With SimpleDateFormat

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

public class Example1 {

    public static void main(String[] args) {

        System.out.println(checkIfDateIsValid("2019-10-13")); // true
        System.out.println(checkIfDateIsValid("2019:10:13")); // false
    }

    private static boolean checkIfDateIsValid(String date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        // With lenient parsing, the parser may use heuristics to interpret
        // inputs that do not precisely match this object's format.
        format.setLenient(false);
        try {
            format.parse(date);
        } catch (ParseException e) {
            return false;
        }
        return true;
    }
}

Output:

true
false

3. With SimpleDateFormat + different patterns

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

public class Example2 {

    public static void main(String[] args) {

        System.out.println("# Example 1");
        System.out.println(checkIfDateIsValid("yyyy-MM-dd", "2019-10-13")); // true
        System.out.println(checkIfDateIsValid("yyyy-MM-dd", "2019:10:13")); // false

        System.out.println("# Example 2");
        System.out.println(checkIfDateIsValid("yyyy/MM/dd", "2019/10/13")); // true

        System.out.println("# Example 3");
        System.out.println(checkIfDateIsValid("yyyy:MM:dd", "2019:10:13")); // true

        System.out.println("# Example 4");
        System.out.println(checkIfDateIsValid("MM/dd/yyyy", "10/13/2019")); // true
    }

    private static boolean checkIfDateIsValid(String pattern, String date) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        // With lenient parsing, the parser may use heuristics to interpret
        // inputs that do not precisely match this object's format.
        format.setLenient(false);
        try {
            format.parse(date);
        } catch (ParseException e) {
            return false;
        }
        return true;
    }
}

Output:

# Example 1
true
false
# Example 2
true
# Example 3
true
# Example 4
true

4. LocalDate - Java 8

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

public class Example3 {

    public static void main(String[] args) {

        System.out.println(checkIfDateIsValid("2019-10-13")); // true
        System.out.println(checkIfDateIsValid("2019:10:13")); // false
    }

    private static boolean checkIfDateIsValid(String date) {
        try {
            LocalDate.parse(date, DateTimeFormatter.ISO_DATE);
        } catch (DateTimeParseException e) {
            return false;
        }
        return true;
    }
}

Output:

true
false

5. LocalDate + DateTimeFormatter build-in patterns

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

public class Example4 {

    public static void main(String[] args) {

        System.out.println("# Example 1");
        System.out.println(checkIfDateIsValid(
                DateTimeFormatter.ISO_DATE, "2019-10-13")); // true

        System.out.println(checkIfDateIsValid(
                DateTimeFormatter.ISO_DATE, "2019:10:13")); // false

        System.out.println("# Example 2");
        System.out.println(checkIfDateIsValid(
                DateTimeFormatter.BASIC_ISO_DATE, "20191013")); // true

        System.out.println("# Example 3");
        System.out.println(checkIfDateIsValid(
                DateTimeFormatter.ISO_LOCAL_DATE, "2019-10-13")); // true
    }

    private static boolean checkIfDateIsValid(DateTimeFormatter formatter, String date) {
        try {
            LocalDate.parse(date, formatter);
        } catch (DateTimeParseException e) {
            return false;
        }
        return true;
    }
}

Output:

# Example 1
true
false
# Example 2
true
# Example 3
true

6. LocalDate + DateTimeFormatter different patterns

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

public class Example5 {

    public static void main(String[] args) {

        System.out.println("# Example 1");
        System.out.println(checkIfDateIsValid("yyyy-MM-dd", "2019-10-13")); // true
        System.out.println(checkIfDateIsValid("yyyy-MM-dd", "2019:10:13")); // false

        System.out.println("# Example 2");
        System.out.println(checkIfDateIsValid("yyyy/MM/dd", "2019/10/13")); // true

        System.out.println("# Example 3");
        System.out.println(checkIfDateIsValid("yyyy:MM:dd", "2019:10:13")); // true

        System.out.println("# Example 4");
        System.out.println(checkIfDateIsValid("MM/dd/yyyy", "10/13/2019")); // true
    }

    private static boolean checkIfDateIsValid(String pattern, String date) {
        try {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            LocalDate.parse(date, formatter);
        } catch (DateTimeParseException e) {
            return false;
        }
        return true;
    }
}

Output:

# Example 1
true
false
# Example 2
true
# Example 3
true
# Example 4
true

References

  1. SimpleDateFormat - JavaDoc
  2. LocalDate - JavaDoc
  3. DateTimeFormatter - JavaDoc
Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.