How to convert a string with extra characters

89 Views Asked by At

How to convert a string with extra characters

String str = "file-09-01-2024"

pattern = "dd-MM-YYYY"

using SimpleDateFormat with pattern = "dd-MM-YYYY"

I don't know how many extra symbols there can be

What if the extra characters are at the end? What if you don't know where it is? "file-09-01-2024-version1" "09-01-2024-index" "09-01-2024" All we know is that the date is in the String, but we don’t know where

2

There are 2 best solutions below

5
Ricola On

Use a regex to extract the dd-MM-YYYY part. A simple regex to match it could be \d{2}-\d{2}-\d{4}. This matches 2 digits, a hyphen, 2 digits, a hyphen and then 4 digits. You can test it on regex101 : https://regex101.com/r/socnHp/1.

Example:

String str = "file-09-01-2024"
Matcher matcher = Pattern.compile("\\d{2}-\\d{2}-\\d{4}").matcher(str);
matcher.find();
String mmddyy = matcher.group(); // this gets the first substring that matches the regex
Date parsed = new SimpleDateFormat("dd-MM-yyyy").parse(mmddyy);

Note

The regex I provided will still match incorrect dates, like 09-14-2024. I believe in for your use case it's fine but if you need stricter validation, you can check similar questions on this site, for example this one.

3
Basil Bourque On

Substring

Use the String#substring method to extract the relevant portion at the end.

String input = "file-09-01-2024" ;
String datePortion = input.substring( input.length() - 10 ) ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MM-uuuu" ) ;
LocalDate ld = LocalDate.parse( datePortion , f ) ;

See this code run at Ideone.com.

Of course the ideal solution would be educating the publisher of your data to:

  • Share data as properly delimited text.
  • Use standard ISO 8601 formats only when exchanging date-time values. For a date, that is YYYY-MM-DD.

Avoid legacy date-time classes

using SimpleDateFormat

Never use SimpleDateFormat, Date, Calendar, etc.

These terribly flawed classes are now legacy, years ago supplanted by the modern java.time classes defined in JSR 310.