Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the time separator symbol in Java?

Tags:

java

time

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?

like image 977
m_pGladiator Avatar asked Apr 08 '10 10:04

m_pGladiator


2 Answers

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

if (text.charAt(++pos.index) != ':')
like image 113
Bozho Avatar answered Nov 14 '22 04:11

Bozho


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;
}
like image 2
rpaul Avatar answered Nov 14 '22 04:11

rpaul