Skip to content
Advertisement

Strip date from String with junk data – Java

I need to know if there is anyway to strip the date alone from text like below using java. I am trying to find something more generic, but couldn’t get any help, As the input varies.

Some example inputs:

This time is  Apr.19,2021 Cheers

or

19-04-2021 Cheers

or

This time is  19-APR-2021

I have seen some code which works for trailing junk characters but couldn’t find anything if the date is in between the string and if it varies to different formats.

Advertisement

Answer

We could use String#replaceAll here for a regex one-liner approach:

String[] inputs = new String[] {
    "This time is  Apr.19,2021 Cheers",
    "19-04-2021 Cheers",
    "This time is  19-APR-2021",
    "Hello 2021-19-Apr World"
};
for (String input : inputs) {
    String date = input.replaceAll(".*(?<!\S)(\S*\b\d{4}\b\S*).*", "$1");
    System.out.println(date);
}

This prints:

Apr.19,2021
19-04-2021
19-APR-2021
2021-19-Apr
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement