2015-09-21 55 views
1

我想知道是否有方法來獲取系統當前正在使用的格式。 截至目前我使用約達時間庫和手動指定,我期待的日期是在foramt。Android日期時間格式模式

private final DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd"); 
LocalDate date = LocalDate.parse(event_date, formatter); 

這當然不是最好的辦法,以硬編碼的方式,所以是有辦法我可以從系統中獲取模式?

+0

檢查DateUtils http://developer.android.com/reference/android/text/format/ DateUtils.html – iago

回答

0

您可以使用這樣的事情:

DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault()); 
String pattern  = ((SimpleDateFormat)formatter).toPattern(); 
String localPattern = ((SimpleDateFormat)formatter).toLocalizedPattern(); 
+0

試過這種方式,它返回MMMM模式,在我的情況下,不符合實際存儲日期值的格式。如果您需要更多詳細信息,我正嘗試從聯繫人中掃描事件手機和爲此我需要在掃描時提供日期格式。 –

+0

它會返回您的默認日期模式,因此MMMM d,y是默認模式。 –

0

你可以這樣做:

String datePattern; 
String timePattern; 
void loadDateTimePattern(Context context) { 
     SimpleDateFormat dateFormat = (SimpleDateFormat) DateFormat.getDateFormat(context); 
     SimpleDateFormat timeFormat = (SimpleDateFormat) DateFormat.getTimeFormat(context); 
     datePattern = dateFormat.toPattern(); 
     timePattern = timeFormat.toPattern(); 
} 

DateTimeFormatter timeFormatter = DateTimeFormat.forPattern(timePattern); 
DateTimeFormatter timeFormatterFull = DateTimeFormat.forPattern(datePattern + ", " + timePattern); 
+0

在日期格式的情況下返回M/d/y,但不符合實際在日期中的日期格式,因此它不起作用。 –