Is there a way to get the time separator symbol ':' in Java? Is it a constant somewhere or a getter? Maybe there is something equivalent to the File.separator?
My time string is returned by DateFormat.getTimeInstance(DateFormat.SHORT, locale).format(date);
Is it safe to just use ':' in this case when later somebody wants to parse this string?
I think it's safe to use ':'
. It is hardcoded in SimpleDateFormat
. For example:
if (text.charAt(++pos.index) != ':')
Try the following
public static String getTimeSeparator(Locale locale) {
String sepValue = ":";
DateFormat dateFormatter = DateFormat.getTimeInstance(DateFormat.SHORT,
locale);
DateFormatSymbols dfs = new DateFormatSymbols(locale);
Date now = new Date();
String[] amPmStrings = dfs.getAmPmStrings();
String localeTime = dateFormatter.format(now);
//remove the am pm string if they exist
for (int i = 0; i < amPmStrings.length; i++) {
localeTime = localeTime.replace(dfs.getAmPmStrings()[i], "");
}
// search for the character that isn't a digit.
for (int currentIndex = 0; currentIndex < localeTime.length(); currentIndex++) {
if (!(Character.isDigit(localeTime.charAt(currentIndex))))
{
sepValue = localeTime.substring(currentIndex, currentIndex + 1);
break;
}
}
return sepValue;
}
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