获取Android系统的首选日期格式字符串

And*_*der 28 android

有人在使用时格式化日期时知道如何获取系统使用的格式字符串

DateFormat.getLongDateFormat(Context context).format(Date date)
Run Code Online (Sandbox Code Playgroud)

Mar*_*ing 69

要获得日期格式模式,您可以执行以下操作:

Format dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
String pattern = ((SimpleDateFormat) dateFormat).toLocalizedPattern();
Run Code Online (Sandbox Code Playgroud)

  • 我可以确认这很有效! (2认同)

toa*_*ran 6

我写了一个方法来检测这个格式字符串.(为我的案子工作).

public static String getDateFormat(Context context){
        // 25/12/2013
        Calendar testDate = Calendar.getInstance();
        testDate.set(Calendar.YEAR, 2013);
        testDate.set(Calendar.MONTH, Calendar.DECEMBER);
        testDate.set(Calendar.DAY_OF_MONTH, 25);

        Format format = android.text.format.DateFormat.getDateFormat(context);
        String testDateFormat = format.format(testDate.getTime());
        String[] parts = testDateFormat.split("/");
        StringBuilder sb = new StringBuilder();
        for(String s : parts){
            if(s.equals("25")){
                sb.append("dd/");
            }
            if(s.equals("12")){
                sb.append("MM/");
            }
            if(s.equals("2013")){
                sb.append("yyyy/");
            }
        }
        return sb.toString().substring(0, sb.toString().length()-1);
    }
Run Code Online (Sandbox Code Playgroud)

编辑请检查Mark Melling在/sf/answers/1328798971/下面的答案以获得更好的解决方案.我很久以前就是一个黑客.


小智 5

API中有一个静态方法,您可以像这样调用:

Format dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
Run Code Online (Sandbox Code Playgroud)

有一个关于它更多的讨论在这里.