2012-06-19 64 views
36

我想基于用户区域设置显示出生日期。在我的应用程序中,我的一个领域是出生日期,现在格式为dd/mm/yyyy。因此,如果用户更改其语言环境,则日期格式也应相应更改。任何指针或代码示例都会帮助我克服这个问题。基于Android的用户区域设置的日期格式

回答

62

您可以使用DateFormat类,该类根据用户区域设置格式化日期。

例子:

String dateOfBirth = "26/02/1974"; 
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 
Date date = null; 
try { 
    date = sdf.parse(dateOfBirth); 
} catch (ParseException e) { 
    // handle exception here ! 
} 
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(context); 
String s = dateFormat.format(date); 

您可以使用不同的方法getLongDateFormatgetMediumDateFormat根据详细程度,你想有。

+3

“日期格式的日期格式” 确实应该写“java.text.DateFormat中日期格式”。有来自不同库的不同版本的DateFormat。 – AndroidDev

+1

查看源代码,'android.text.format.DateFormat.getDateFormat(context)'只返回'java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT)'。实际上,甚至没有使用'context'参数。我想我更喜欢直接使用'java.text.DateFormat.getDateInstance(DateFormat.SHORT)'。 –

+0

我没有字符串类型的输入日期,我有日期作为输入 – user924

11

要根据区域设置更改日期格式,下面的代码工作对我说:

String dateOfBirth = "26/02/1974"; 
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 
    Date date = null; 
    try { 
     date = sdf.parse(dateOfBirth); 
    } catch (ParseException e) { 
     // handle exception here ! 
    } 

    String myString = DateFormat.getDateInstance(DateFormat.SHORT).format(date); 

然后,当你改变了语言环境,日期格式将改变基于它。 有关日期模式的更多信息: http://docs.oracle.com/javase/tutorial/i18n/format/dateFormat.html

+0

您提供的链接对我很有用。谢谢 –

+0

也一样!谢谢 ! –

6

Android的API提供简单的方法来显示本地化的日期格式。 以下代码有效。

public class FileDateUtil { 
public static String getModifiedDate(long modified) { 
    return getModifiedDate(Locale.getDefault(), modified); 
} 

public static String getModifiedDate(Locale locale, long modified) { 
    SimpleDateFormat dateFormat = null; 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { 
     dateFormat = new SimpleDateFormat(getDateFormat(locale)); 
    } else { 
     dateFormat = new SimpleDateFormat("MMM/dd/yyyy hh:mm:ss aa"); 
    } 

    return dateFormat.format(new Date(modified)); 
} 

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) 
public static String getDateFormat(Locale locale) { 
    return DateFormat.getBestDateTimePattern(locale, "MM/dd/yyyy hh:mm:ss aa"); 
} 
} 

您可以查看下面的代码。

String koreaData = FileDateUtil.getModifiedDate(Locale.KOREA, System.currentTimeMillis()); 
String franceData = FileDateUtil.getModifiedDate(Locale.FRENCH, System.currentTimeMillis()); 
String defaultData = FileDateUtil.getModifiedDate(Locale.getDefault(), System.currentTimeMillis()); 

String result = "Korea : " + koreaData + System.getProperty("line.separator"); 
result += "France : " + franceData + System.getProperty("line.separator"); 
result += "Default : " + defaultData + System.getProperty("line.separator"); 
tv.setText(result); 
5

就这么简单

对于日期+时间:

DateFormat format = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.SHORT, Locale.getDefault()); 

对于刚刚日期:

DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault()); 
+0

只是为了纠正复制粘贴错误:对于没有时间的日期,调用getDateInstance(而不是getDateTimeInstance) – Lensflare

相关问题