EN
Java - how to check if String is valid date?
16 points
In this post we can find code which can validate if String contains valid Date based on defined pattern.
xxxxxxxxxx
1
import java.text.ParseException;
2
import java.text.SimpleDateFormat;
3
4
public class Example1 {
5
6
public static void main(String[] args) {
7
8
System.out.println(checkIfDateIsValid("2019-10-13")); // true
9
System.out.println(checkIfDateIsValid("2019:10:13")); // false
10
}
11
12
private static boolean checkIfDateIsValid(String date) {
13
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
14
// With lenient parsing, the parser may use heuristics to interpret
15
// inputs that do not precisely match this object's format.
16
format.setLenient(false);
17
try {
18
format.parse(date);
19
} catch (ParseException e) {
20
return false;
21
}
22
return true;
23
}
24
}
Output:
xxxxxxxxxx
1
true
2
false
xxxxxxxxxx
1
import java.text.ParseException;
2
import java.text.SimpleDateFormat;
3
4
public class Example2 {
5
6
public static void main(String[] args) {
7
8
System.out.println("# Example 1");
9
System.out.println(checkIfDateIsValid("yyyy-MM-dd", "2019-10-13")); // true
10
System.out.println(checkIfDateIsValid("yyyy-MM-dd", "2019:10:13")); // false
11
12
System.out.println("# Example 2");
13
System.out.println(checkIfDateIsValid("yyyy/MM/dd", "2019/10/13")); // true
14
15
System.out.println("# Example 3");
16
System.out.println(checkIfDateIsValid("yyyy:MM:dd", "2019:10:13")); // true
17
18
System.out.println("# Example 4");
19
System.out.println(checkIfDateIsValid("MM/dd/yyyy", "10/13/2019")); // true
20
}
21
22
private static boolean checkIfDateIsValid(String pattern, String date) {
23
SimpleDateFormat format = new SimpleDateFormat(pattern);
24
// With lenient parsing, the parser may use heuristics to interpret
25
// inputs that do not precisely match this object's format.
26
format.setLenient(false);
27
try {
28
format.parse(date);
29
} catch (ParseException e) {
30
return false;
31
}
32
return true;
33
}
34
}
Output:
xxxxxxxxxx
1
# Example 1
2
true
3
false
4
# Example 2
5
true
6
# Example 3
7
true
8
# Example 4
9
true
xxxxxxxxxx
1
import java.time.LocalDate;
2
import java.time.format.DateTimeFormatter;
3
import java.time.format.DateTimeParseException;
4
5
public class Example3 {
6
7
public static void main(String[] args) {
8
9
System.out.println(checkIfDateIsValid("2019-10-13")); // true
10
System.out.println(checkIfDateIsValid("2019:10:13")); // false
11
}
12
13
private static boolean checkIfDateIsValid(String date) {
14
try {
15
LocalDate.parse(date, DateTimeFormatter.ISO_DATE);
16
} catch (DateTimeParseException e) {
17
return false;
18
}
19
return true;
20
}
21
}
Output:
xxxxxxxxxx
1
true
2
false
xxxxxxxxxx
1
import java.time.LocalDate;
2
import java.time.format.DateTimeFormatter;
3
import java.time.format.DateTimeParseException;
4
5
public class Example4 {
6
7
public static void main(String[] args) {
8
9
System.out.println("# Example 1");
10
System.out.println(checkIfDateIsValid(
11
DateTimeFormatter.ISO_DATE, "2019-10-13")); // true
12
13
System.out.println(checkIfDateIsValid(
14
DateTimeFormatter.ISO_DATE, "2019:10:13")); // false
15
16
System.out.println("# Example 2");
17
System.out.println(checkIfDateIsValid(
18
DateTimeFormatter.BASIC_ISO_DATE, "20191013")); // true
19
20
System.out.println("# Example 3");
21
System.out.println(checkIfDateIsValid(
22
DateTimeFormatter.ISO_LOCAL_DATE, "2019-10-13")); // true
23
}
24
25
private static boolean checkIfDateIsValid(DateTimeFormatter formatter, String date) {
26
try {
27
LocalDate.parse(date, formatter);
28
} catch (DateTimeParseException e) {
29
return false;
30
}
31
return true;
32
}
33
}
Output:
xxxxxxxxxx
1
# Example 1
2
true
3
false
4
# Example 2
5
true
6
# Example 3
7
true
xxxxxxxxxx
1
import java.time.LocalDate;
2
import java.time.format.DateTimeFormatter;
3
import java.time.format.DateTimeParseException;
4
5
public class Example5 {
6
7
public static void main(String[] args) {
8
9
System.out.println("# Example 1");
10
System.out.println(checkIfDateIsValid("yyyy-MM-dd", "2019-10-13")); // true
11
System.out.println(checkIfDateIsValid("yyyy-MM-dd", "2019:10:13")); // false
12
13
System.out.println("# Example 2");
14
System.out.println(checkIfDateIsValid("yyyy/MM/dd", "2019/10/13")); // true
15
16
System.out.println("# Example 3");
17
System.out.println(checkIfDateIsValid("yyyy:MM:dd", "2019:10:13")); // true
18
19
System.out.println("# Example 4");
20
System.out.println(checkIfDateIsValid("MM/dd/yyyy", "10/13/2019")); // true
21
}
22
23
private static boolean checkIfDateIsValid(String pattern, String date) {
24
try {
25
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
26
LocalDate.parse(date, formatter);
27
} catch (DateTimeParseException e) {
28
return false;
29
}
30
return true;
31
}
32
}
Output:
xxxxxxxxxx
1
# Example 1
2
true
3
false
4
# Example 2
5
true
6
# Example 3
7
true
8
# Example 4
9
true