2011-11-03 7 views
0

我在米利斯一個時間,我需要顯示給用戶在一個特定的可讀格式,這取決於當前設備配置:的Android DateFormat的特定格式*和*尊重當前用戶偏好

java.text.DateFormat mDateFormat = android.text.format.DateFormat.getDateFormat(this.getApplicationContext()); 
java.text.DateFormat mTimeFormat = android.text.format.DateFormat.getTimeFormat(this.getApplicationContext()); 

我使用String mDateFormat.format(java.util.Date myFooDate)來檢索它。

然而,它只返回 「31/12/2011」(或12/31/2011取決於環境)。

我想它是 「孫31/12」(或 「孫12/31」,自動當然...)。

打電話給我很愚蠢,但我找不到選項(大約1小時後)...我只發現硬編碼字符串格式的選項(使用那些「MM」,「HH」和相似),但我正如我所說,我希望它尊重當前設備偏好在該特定格式。如果用戶使用月份/日期,我不想做不同的事情。

謝謝。

+0

感謝彼得O.和DMON,我會稍後檢查,並返回。 – davidcesarino

回答

2

在你的情況,你應該檢查mDateFormat的值。如果它是SimpleDateFormat,則可以將mDateFormat轉換爲SimpleDateFormat並調用toPattern()方法。然後檢查結果字符串中的大寫M是否後跟小寫D(不一定立即),反之亦然。這可以幫助指導您使用的特定格式。

以下是完整的源代碼。我把它放在公共領域。

public static boolean isMonthBeforeDay(DateFormat df){ 
    if(df instanceof SimpleDateFormat){ 
     SimpleDateFormat sdf=(SimpleDateFormat)df; 
     String pattern=sdf.toPattern(); 
     int i=0; 
     int dayFound=-1; 
     int monthFound=-1; 
     while(i<pattern.length()){ 
      if(pattern.charAt(i)=='\''){ 
       // Ignore quoted text 
       i++; 
       while(i<pattern.length()){ 
        if(pattern.charAt(i)=='\''){ 
         // Possible end of quoted text 
         if(i+1>=pattern.length()) 
          break; 
         else if(pattern.charAt(i+1)=='\''){ 
          i++; 
         } else { 
          i++; 
          break; 
         } 
        } 
        i++; 
       } 
       continue; 
      } 
      if(pattern.charAt(i)=='M' && monthFound<0){ 
       monthFound=i; 
      } 
      else if(pattern.charAt(i)=='d' && dayFound<0){ 
       dayFound=i; 
      } 
      i++; 
     } 
     if(monthFound>=0 && dayFound>=0){ 
      // Found both month and day in pattern 
      return (monthFound<dayFound); 
     } 
     // Assume true, you can change to false if you want 
     // the day to come before the month by default 
     return true; 
    } else { 
     StringBuffer sb=new StringBuffer(); 
     FieldPosition fpMonth=new FieldPosition(DateFormat.MONTH_FIELD); 
     FieldPosition fpDay=new FieldPosition(DateFormat.DATE_FIELD); 
     GregorianCalendar gc=new GregorianCalendar(2000,0,20); 
     Date d=gc.getTime(); 
     // Find the field position of the month 
     df.format(d,sb,fpMonth); 
     sb.delete(0, sb.length()); 
     // Find the field position of the day 
     df.format(d,sb,fpDay); 
     return fpMonth.getBeginIndex()<fpDay.getBeginIndex(); 
    } 
} 
+1

一個全面的解決方案是通過它提供已知的明確日期和時間,並檢查輸出中的月份或日期是否先到達。 – Jens

+0

接受答案。你和dmon提供了非常好的答案,他甚至提到需要從模式中刪除年份。我不知道如何使用dateFormat(我知道如何使用模式本身......)。你的回答差異在於你讓我看到了這些值並投射到了SimpleDateFormat,而我並沒有意識到這一點。從那裏,我習慣了模式,用我已有的知識取代了年份模式。謝謝! – davidcesarino

1

我不認爲你可以得到你想要的格式(似乎並沒有成爲一個共同的模式要麼)無論從Android的DateFormatDateUtils類。我相信你將不得不自己組裝它。如果您始終希望自己的DAY_OF_WEEK在前面,那麼從獲得的格式對象中取出日期格式字符串(請致電toPattern()),刪除年份部分(/yyyyyyyy/),並將其與EEE連接以創建格式字符串。

0

用於生成格式化日期/時間字符串的實用工具類。

該類將輸入格式字符串和日期/時間的表示。格式字符串控制如何生成輸出。

可能會重複格式化字符以獲取該字段的更詳細的表示。例如,格式字符'M'用於表示月份。取決於角色重複的次數,你會得到不同的表示。

​​3210

複製的效果取決於場的性質而變化。有關詳細信息,請參閱個別字段格式化程序中的說明。對於純數字字段(如HOUR),添加更多指示符的副本將將值填充到該字符數。

For 7 minutes past the hour: 

m -> 7 

mm -> 07 

mmm -> 007 

mmmm -> 0007 

而且

Examples for April 6, 1970 at 3:23am: 

"MM/dd/yy h:mmaa" -> "04/06/70 3:23am" 

"MMM dd, yyyy h:mmaa" -> "Apr 6, 1970 3:23am" 

"MMMM dd, yyyy h:mmaa" -> "April 6, 1970 3:23am" 

"E, MMMM dd, yyyy h:mmaa" -> "Mon, April 6, 1970 3:23am& 

"EEEE, MMMM dd, yyyy h:mmaa" -> "Monday, April 6, 1970 3:23am" 

"'Noteworthy day: 'M/d/yy" -> "Noteworthy day: 4/6/70" 

問候,Mehul Patel

+0

再次閱讀問題,你會看到我沒有問這個問題,但是謝謝你的時間。 – davidcesarino