Answers for "how to verify dates java"

2

validate date java

The current way is to use the calendar class. It has the setLenient method that will validate the date and throw and exception if it is out of range as in your example.

Forgot to add: If you get a calendar instance and set the time using your date, this is how you get the validation.

Calendar cal = Calendar.getInstance();
cal.setLenient(false);
cal.setTime(yourDate);
try {
    cal.getTime();
}
catch (Exception e) {
  System.out.println("Invalid date");
}
Posted by: Guest on March-24-2021
0

test date in java

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd-MM-yyyy");
String inputString1 = "23-01-1997";
String inputString2 = "27-04-1997";

try {
    LocalDateTime date1 = LocalDate.parse(inputString1, dtf);
    LocalDateTime date2 = LocalDate.parse(inputString2, dtf);
    long daysBetween = Duration.between(date1, date2).toDays();
    System.out.println ("Days: " + daysBetween);
} catch (ParseException e) {
    e.printStackTrace();
}
Posted by: Guest on June-30-2021

Code answers related to "how to verify dates java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language