0

I know there are multiple questions asking to verify a valid date. But I was unable to find out the exact format. So please do not mark this as a duplicate.

I have a date returned as a string E.g., 2 Sep 2018 09:00 in my web page. I need to verify this is a date as a part of my selenium tests. Appreciate if someone could help me to verify this as a valid date format in Java.

Thanks

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Ushani
  • 1,199
  • 12
  • 28

2 Answers2

2

Require a standard format for your user’s locale

    DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(
                    FormatStyle.MEDIUM, FormatStyle.SHORT)
            .withLocale(Locale.UK);
    ZoneId userTimeZone = ZoneId.of("Europe/London");
    // Require a comma between date and time
    String returnedFromWebPage = "2 Sep 2018, 09:00";
    // Remove any space before the date or after the time
    returnedFromWebPage = returnedFromWebPage.trim();
    try {
        ZonedDateTime dateTime = LocalDateTime.parse(returnedFromWebPage, formatter)
                .atZone(userTimeZone);
        if (dateTime.isBefore(ZonedDateTime.now(userTimeZone))) {
            System.out.println("A valid date time");
        } else {
            System.out.println("Not in the past");
        }
    } catch (DateTimeParseException dtpe) {
        System.out.println("Not a valid date time format");
    }

Output when run on Java 10:

A valid date time

Java 10 with default locale data thinks that the a date and time notation in the UK may go like 2 Sep 2018, 09:00 (depending on how long or short you want it), that is, with a comma between date and time, otherwise like your input string. So one suggestion is to see whether your users could agree to that and enter the date and time in this way. If it is correct that this follows UK norms, I figure they’ll be happy to.

Now I don’t know whether your users are British at all. Java has localized formats for hundreds of locales. I think that you should first of all use your users’ locale. If they happen to speak Swahili, the standard format seems to be without the comma:

    DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(
                    FormatStyle.MEDIUM, FormatStyle.SHORT)
            .withLocale(Locale.forLanguageTag("sw"));
    ZoneId userTimeZone = ZoneId.of("Africa/Nairobi");
    String returnedFromWebPage = "2 Sep 2018 09:00";

With these changes the code also prints A valid date time.

If required build your own formatter

If your users are not happy with any of the built-in formats in Java, you will need to specify the format they want to use:

    DateTimeFormatter formatter 
            = DateTimeFormatter.ofPattern("d MMM uuuu HH:mm", Locale.ENGLISH);
    String returnedFromWebPage = "2 Sep 2018 09:00";

This will also cause the code to print A valid date time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

I found a way. Posting since it will be useful.

            String dateformat = "A valid date time format";
    String notValidDateFormat = "Not a valid date time format" ;
    final DateFormat fmt = new SimpleDateFormat("dd MMM yyyy hh:mm");
    Date input = null;
    try {
        input = fmt.parse(offerDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    if (input.before(new Date())) {
        return dateformat;
    }
    return notValidDateFormat;
}
Ushani
  • 1,199
  • 12
  • 28
  • 2
    Use the `java.time` package for dealing with dates and times – OneCricketeer Sep 19 '18 at 23:52
  • 1
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Sep 20 '18 at 07:43
  • 1
    Also it doesn’t provide very good validation. If I write `Sept` instead of `Sep`, it crashes rather than reporting `Not a valid date time format`. It accepts `35 May 2018 91:92` and `2 Sep 2018 09:00, a Tuesday`. – Ole V.V. Sep 20 '18 at 07:51