0

Try to have a function that can validate a input date string. For example 201807192113 indicates 2018/07/09 21:13

private static final SimpleDateFormat CASSANDRA_DATE_FORMAT = new SimpleDateFormat("YYYYMMddHHmm");

private static boolean isValidateDate(final String input) {

    try {
        CASSANDRA_DATE_FORMAT.setLenient(false);
        final Date date = CASSANDRA_DATE_FORMAT.parse(input);
        System.out.println(date.toString());

        return true;
    } catch (ParseException e) {
        e.printStackTrace();
        return false;
    }
}

public static void main(String[] args) {

    final boolean validateDate = FCXTablesValidation.isValidateDate("201807200101");
    System.out.println(validateDate);
}

While the output is Sun Dec 31 21:13:00 EST 2017 true, which indicates another date.

Also, if passed in 201807322113 which is not a valid date(32 is not a valid day in a month), still got the same output.

chrisTina
  • 2,298
  • 9
  • 40
  • 74
  • You are using `"YYYY"` as the format of year. `Y` is the pattern letter for [week of year](https://docs.oracle.com/javase/10/docs/api/java/util/GregorianCalendar.html#week_year). To be honest I don't know how the mentioned output is parsed from the input string. But if you use the pattern letter `y`the lenient setting will throw a `ParseException`. So is it intended to use the pattern letter `Y`? – LuCio Jul 25 '18 at 20:03
  • Are you saying that if you pass `"201807200101"` to this method, it prints `Sun Dec 31 21:13:00 EST 2017`? Are you sure? – Dawood ibn Kareem Jul 25 '18 at 20:12
  • @LuCio - Actually `Y` is "week year", not "week of year". Lower case `w` is "week of year". – Dawood ibn Kareem Jul 25 '18 at 20:15
  • @DawoodibnKareem Yes, you're right. I should have copy&paste it ;) "week year" is also where I have linked to using the link in the JavaDoc of `SimpleDateFormat`. But nonetheless, your objection is justified. – LuCio Jul 25 '18 at 20:48
  • 1
    FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jul 26 '18 at 00:27
  • 1
    Duplicates of [this](https://stackoverflow.com/q/46581387/642706), [this](https://stackoverflow.com/q/28858787/642706), [this](https://stackoverflow.com/q/49763466/642706), and [this](https://stackoverflow.com/q/13868251/642706). [One of my own Answers](https://stackoverflow.com/a/48939890/642706) has the exact code snippet you need. – Basil Bourque Jul 26 '18 at 00:29
  • I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jul 26 '18 at 08:06

0 Answers0