2013-03-15 88 views
5

我具有由檢索到以毫秒爲單位指定的日期/時間:格式日期/時間,以毫秒爲單位的本地串中的Android

Calendar.getInstance().getTimeInMillis(); 

我想這種格式表示一個串無論本地格式在設備上的日期和時間。例如:

US:  12/15/2013 10:30 pm 
Germany: 15.12.2013 22:30 

我不應該指定格式如:

SimpleDateFormat outputFormat = new SimpleDateFormat("MMM dd, yyyy h:mm a"); 

是否有處理本地格式的API?

回答

0

我想這是你在找什麼:

Locale locale = Locale.getDefault();  
Date today = new Date(); 
SimpleDateFormat.SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.LONG, SimpleDateFormat.LONG, locale).format(today)); 

,你需要檢查locale,並相應地傳遞其價值。

+0

我的時間以毫秒爲單位。它可能不是「今天」。可能是任何時間。 Date的值是幾毫秒的相同值嗎? – AndroidDev 2013-03-15 09:17:04

+0

是的,和new Date()一樣,它也使用毫秒。 – dShringi 2013-03-15 09:18:53

+0

這不包括時間。它只提供日期。 – AndroidDev 2013-03-15 10:38:06

-2

作爲一個選項,你可以存儲格式字符串在字符串資源,使其可以通過

getResources().getString(R.string.format) 

然後,只需把你需要語言環境文件夾的字符串,例如:

res/values-en/strings.xml 
res/values-fr/strings.xml 
0

見本代碼

String myformat = android.provider.Settings.System.getString(getContentResolver(),  android.provider.Settings.System.DATE_FORMAT); 
    DateFormat dateFormat; 
    if (TextUtils.isEmpty(myformat)) { 
     dateFormat = android.text.format.DateFormat.getMediumDateFormat(getApplicationContext()); 
    } else { 
     dateFormat = new SimpleDateFormat(format); 
    } 

這樣的日期格式將匹配您的設備日期

0

檢查DateFormat。特別是

public static final DateFormat getDateInstance(int style, 
               Locale aLocale) 

這裏是一個example。看看它是否可以幫助。

相關問題