如何在Java中获取时间分隔符?

如何在Java中获取时间分隔符?,java,time,Java,Time,有没有办法在Java中获取时间分隔符“:”呢?它是某个地方的常数还是一个getter?可能存在与File.separator等效的内容 我的时间字符串由DateFormat.getTimeInstance(DateFormat.SHORT,locale).format(date)返回 在这种情况下,当以后有人想解析这个字符串时,只使用“:”安全吗?我认为使用“”:“是安全的。它是在SimpleDateFormat中硬编码的。例如: if (text.charAt(++pos.index) !=

有没有办法在Java中获取时间分隔符“:”呢?它是某个地方的常数还是一个getter?可能存在与File.separator等效的内容

我的时间字符串由
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;
}
公共静态字符串getTimeSeparator(区域设置){
字符串sepValue=“:”;
DateFormat dateFormatter=DateFormat.getTimeInstance(DateFormat.SHORT,
地点);
DateFormatSymbols dfs=新的DateFormatSymbols(区域设置);
现在日期=新日期();
字符串[]amPmStrings=dfs.getAmPmStrings();
字符串localeTime=dateFormatter.format(现在);
//删除am pm字符串(如果存在)
对于(int i=0;i