0

Hi I have a string that looks like this - Wednesday 16 January 2019. I want to check in groovy if this date matches with the format EEEE DD MMMM YYYY. Is there a way to do this?

Are there any inbuilt functions that I can use or regex is my only option?

Cheers!

xingbin
  • 27,410
  • 9
  • 53
  • 103
Archit Arora
  • 2,508
  • 7
  • 43
  • 69

1 Answers1

1

You can try parse it with pattern EEEE dd MMMM yyyy, if exception is thrown, then it is not in this pattern, in java it looks like:

public boolean isInDesiredFormat(String input) {
    try {
        DateTimeFormatter format = DateTimeFormatter.ofPattern("EEEE dd MMMM yyyy", Locale.ENGLISH);
        LocalDate.parse(input, format);
        return true;
    } catch (Exception ignore) {
        ignore.printStackTrace();
        return false;
    }
}
xingbin
  • 27,410
  • 9
  • 53
  • 103
  • As explained in [this answer](https://stackoverflow.com/a/39649815/5772882) you will probably want to apply `ResolverStyle.STRICT ` for stricter validation. – Ole V.V. Jan 24 '19 at 13:08