2010-04-08 57 views
5

有沒有辦法在Java中獲得時間分隔符符號':'?它在某個地方還是一個吸氣者?也許有相當於File.separator的東西?如何在Java中獲取時間分隔符符號?

我的時間字符串由DateFormat.getTimeInstance(DateFormat.SHORT, locale).format(date);

返回它是安全的,只要用「:」在這種情況下,當後來有人想解析這個字符串?

回答

6

我認爲使用':'是安全的。它在SimpleDateFormat中被硬編碼。例如:

if (text.charAt(++pos.index) != ':') 
2

請嘗試以下

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; 
} 
相關問題