2013-04-09 103 views
1

似乎有许多方法来格式化日期和时间,Android的 - 但没有完全相同的方式我想;-)格式化的日期和时间,在“设置 - 日期和时间”

的方法我已经能够查找涉及区域设置(直接或间接) - 但正确的方法必须是使用用户在“设置 - 日期和时间”中配置的内容。

使用例如:

String res = java.text.DateFormat.getDateTimeInstance(java.text.DateFormat.SHORT, java.text.DateFormat.MEDIUM).format(date); 
       Log.d("", res); 

日志:13年4月9日下午十二时11分34秒

res = android.text.format.DateUtils.formatDateTime(getActivity(), date.getTime(), android.text.format.DateUtils.FORMAT_SHOW_TIME | android.text.format.DateUtils.FORMAT_SHOW_DATE | android.text.format.DateUtils.FORMAT_SHOW_YEAR); 
       Log.d("", res); 

日志:12:11 2013年4月9日

但这不是它如何配置,当我看着一个文件的日期和时间它写为:09/04/2013 12:11

那么如何获得在“设置 - 日期和时间”中配置的格式化字符串(如“yyyy-MM-dd”和“HH:mm”),或者如何获取根据这些日期格式化的日期规则是什么?

回答

2

尝试使用下面的代码:

public static String convertDateToUserFormat(final Date inputDate) { 
if (inputDate != null) { 
    return getDateFormat().format(inputDate); 
} 

return null; 
} 

private static DateFormat getDateFormat() { 
    final String format = Settings.System.getString(context.getContentResolver(), 
      Settings.System.DATE_FORMAT); 
    if (TextUtils.isEmpty(format)) { 
     return android.text.format.DateFormat 
       .getDateFormat(context); 
    } 

    return new SimpleDateFormat(format); 
} 
1
Format dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext()); 
formattedDate = formatter.parse(yourDateString); 

欲了解更多信息的DateFormatter check out the developer doc

+0

据我了解,这将返回一个日期 - 我想走另一条路。 – 2013-04-10 14:31:52

+0

查看编辑答案 – Triode 2013-04-10 15:54:58

-1

这里是

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); 
    Date date = new Date(); 

    System.out.println("format 1 " + sdf.format(date)); 

    sdf.applyPattern("E MMM dd yyyy"); 
    System.out.println("format 2 " + sdf.format(date)); 

U可以按照这个链接,日期格式的例子更多信息enter link description here

+1

是的,但我想使用系统格式 - 不是我自己的。 – 2013-04-10 15:05:28