What's the nicest way to parse a date that can be in one of the following formats
"dd-MM-yyyy HH:mm"
"dd/MM/yyyy HH:mm"
"dd.MM.yyyy HH:mm"
without creating 3 SimpleDateFormats and parsing against each one.
Thanks
It's probably easiest to "tweak" the source string into a canonical format:
if (text.length() == 16)
{
if ((text.charAt(2) == '/' && text.charAt(5) == '/') ||
(text.charAt(2) == '.' && text.charAt(5) == '.'))
{
text = text.substring(0, 2) + "-" + text.substring(3, 5)
+ "-" + text.substring(6);
}
}
Then use the format string using "-".
Note that this is very specific, only replacing exactly the characters you're interested in, to avoid unwanted side-effects.
Could you run two replace operations first, so that you reduce all three formats to a single one?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With