5

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?

m_pGladiator
  • 8,462
  • 7
  • 43
  • 61

2 Answers2

6

I think it's safe to use ':'. It is hardcoded in SimpleDateFormat. For example:

if (text.charAt(++pos.index) != ':')
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
2

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;
}
rpaul
  • 21
  • 1