有没有办法在Java中获得时间分隔符符号':'?它在某个地方还是一个吸气者?也许有相当于File.separator的东西?如何在Java中获取时间分隔符符号?
我的时间字符串由DateFormat.getTimeInstance(DateFormat.SHORT, locale).format(date);
返回它是安全的,只要用“:”在这种情况下,当后来有人想解析这个字符串?
有没有办法在Java中获得时间分隔符符号':'?它在某个地方还是一个吸气者?也许有相当于File.separator的东西?如何在Java中获取时间分隔符符号?
我的时间字符串由DateFormat.getTimeInstance(DateFormat.SHORT, locale).format(date);
返回它是安全的,只要用“:”在这种情况下,当后来有人想解析这个字符串?
我认为使用':'
是安全的。它在SimpleDateFormat
中被硬编码。例如:
if (text.charAt(++pos.index) != ':')
请尝试以下
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;
}