how to validate date format MM/YYYY
as String
example-
09/20 false
13/5651 false
3/2104 true
03/2010 true
how to validate date format MM/YYYY
as String
example-
09/20 false
13/5651 false
3/2104 true
03/2010 true
Use YearMonth
class.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "M/uuuu" ) ;
YearMonth ym = YearMonth.parse( "3/2018" , f ) ;
Trap for DateTimeParseException
to detect invalid input.
Tip: Use standard ISO 8601 formats for exchanging date-time values as text. For year-month: YYYY-MM
The java.time classes use these standard formats by default.
YearMonth.parse( "2018-03" )
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
What you want is a customized method because you place special constraints to the validity of the date.
The following passed all your 4 examples:
public static boolean validDate(String date) {
date = date.trim();
if (date.length() < 6)
return false;
String[] parts = date.split("/");
if (parts.length != 2)
return false;
int month = 0;
try {
month = Integer.parseInt(parts[0]);
} catch (NumberFormatException e) {
e.printStackTrace();
return false;
}
if (month < 1 || month > 12)
return false;
int year = 0;
try {
year = Integer.parseInt(parts[1]);
} catch (NumberFormatException e) {
e.printStackTrace();
return false;
}
if (year < 1000)
return false;
return true;
}
public static void main(String[] args) {
String s1 = "09/20";
String s2 = "13/5651";
String s3 = "3/2104";
String s4 = "03/2010";
System.out.println(s1 + " " + validDate(s1));
System.out.println(s2 + " " + validDate(s2));
System.out.println(s3 + " " + validDate(s3));
System.out.println(s4 + " " + validDate(s4));
}
will print:
09/20 false
13/5651 false
3/2104 true
03/2010 true
I'm not sure about this part:
if (year < 1000)
return false;
you can change it if you like.
public boolean isThisDateValid(String dateToValidate, String dateFromat){
if(dateToValidate == null){
return false;
}
SimpleDateFormat sdf = new SimpleDateFormat(dateFromat);
sdf.setLenient(false);
try {
//if not valid, it will throw ParseException
Date date = sdf.parse(dateToValidate);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
return false;
}
return true;
}
Referensi https://www.mkyong.com/java/how-to-check-if-date-is-valid-in-java/