1

I want to verify if date (java.util.Date) is correct like 2021/02/31 does not exist

 DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
        dateFormat.setLenient(false);
  try{
      dateFormat.parse(dateFormat.format(date));
  } catch(ParseException e) {
     return false;
  }
  return true;
dreamcrash
  • 47,137
  • 25
  • 94
  • 117
koukou
  • 23
  • 6

2 Answers2

5

I recommend you to do it using the modern date-time API*

Use DateTimeFormatter#withResolverStyle(ResolverStyle.STRICT) to resolve a date strictly.

import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.ResolverStyle;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String strDate = "2021/02/31";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd", Locale.ENGLISH)
                .withResolverStyle(ResolverStyle.STRICT);
        try {
            LocalDate date = LocalDate.parse(strDate, dtf);
            // ...
        } catch (DateTimeException e) {
            System.out.println(e.getMessage());
        }
    }
}

Output:

Text '2021/02/31' could not be parsed: Invalid date 'FEBRUARY 31'

Learn more about the modern date-time API from Trail: Date Time.

Also, note that yyyy-mm-dd is not a correct format for your date string which has / instead of - and m is used for a minute, not a month for which you have to use M.


The java.util date-time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API. For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

First of all your date format uses mm which represents minutes, for months it needs to be MM.

Secondly I am not sure what you meant to do with dateFormat.parse(dateFormat.format(date));. The inner part (dateFormat.format(Date)) should be used to convert a date to a string, and the outer part (dateFormat.parse(String)) is to convert a string to a date. So this seems to be a bit of a pointless operation.

I think what you meant was something like this, which should check the validity of date properly:

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
try{
    dateFormat.parse(dateFormat.format("2021-02-31"));
} catch(ParseException e) {
    return false;
}
return true;

Having said this, using the new Java Date-Time APIs is always a better approach, so I would defer to the answer from @ArvindKumarAvinash for a more future-proof solution.

Henry Twist
  • 5,666
  • 3
  • 19
  • 44