24.03.2018 02:34:22 how can I get the delimiter ('.') from a that kind of date string? Format can be different and delimiter can be these '.','-','/' also. Is there a way to do it with Java's date format classes?
To answer this, we'll have to make a few assumptions:
-, /, or ., so I will be assuming no other delimiter is allowed.Therefore, we should be able to solve this by looking for the first occurrence of any of those three characters:
public static Optional<Character> findDelimiter(String formattedDate) {
for (char c : formattedDate.toCharArray()) {
switch (c) {
case '.':
case '/':
case '-':
return Optional.of(c);
case ' ':
case '\t':
return Optional.empty();
}
}
return Optional.empty();
}
This will stop at the first instance of a space or a tab, assuming neither of those is the delimiter.
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