代码之家  ›  专栏  ›  技术社区  ›  m_pGladiator

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

  •  5
  • m_pGladiator  · 技术社区  · 14 年前

    在Java中有时间获取分隔符符号“:”的方法吗?它是一个常数还是一个吸气剂?也许有什么东西与file.separator等价?

    我的时间字符串由返回 DateFormat.getTimeInstance(DateFormat.SHORT, locale).format(date);

    在这种情况下,当有人想解析这个字符串时,使用“:”是否安全?

    2 回复  |  直到 13 年前
        1
  •  6
  •   Bozho    14 年前

    我认为它是安全的 ':' . 它是硬编码的 SimpleDateFormat . 例如:

    if (text.charAt(++pos.index) != ':')
    
        2
  •  2
  •   rpaul    13 年前

    尝试以下操作

    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;
    }