格式化日期和时间取决于用户的语言环境带有几秒钟的偏好 [英] Formatting date & time according to user's locale & preferences with seconds

查看:91
本文介绍了格式化日期和时间取决于用户的语言环境带有几秒钟的偏好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据用户的设置获取格式化的日期(年,月,日)和时间(小时,分钟,秒)字符串。 Android开发者Google网上论坛中的帖子描述了我所遇到的确切问题有,但是没有人帮助那个人解决它。我将对其进行总结:

I am attempting to get a formatted date (year, month, date) and time (hour, minute, second) string according to the user's settings. This post in the Android Developers Google Group describes the precise problem I am having, but no one helped that person solve it. I will summarize it:

Android具有尝试执行上述操作的这些类,但没有一个同时使用用户首选项并显示秒。

Android has these classes that attempt to do the above, but none use both the user preferences and display the seconds.


  1. java.text.DateFormat

    不使用在设置应用中设置的偏好设置

  1. java.text.DateFormat
    Doesn't use preferences set in Settings app

android.text.format.DateFormat

获取一个 java.text.DateFormat 对象,该对象使用<$ c正确格式化输出$ c> getDateFormat()和 getTimeFormat(),但是 getTimeFormat()不会

android.text.format.DateFormat
Gets a java.text.DateFormat object that formats output correctly using getDateFormat() and getTimeFormat(), but getTimeFormat() doesn't include seconds.

android.text.format.DateUtils

不使用设置用于在设置应用中显示日期的首选项,并且没有显示秒的方式。

android.text.format.DateUtils
Doesn't use preferences set for showing date in Settings app and no way to display seconds.

例如,设置中的首选项设置为DD / MM / YYYY和24小时format = on,运行以下代码:

For example, with preferences in Settings set to DD/MM/YYYY and 24-hour format = on, running the following code:

long timestamp=System.currentTimeMillis();

sometextview.setText(
    java.text.format.DateFormat.getDateTimeInstance().format(new Date(timestamp)) 

    +"\n"+ 

    android.text.format.DateFormat.getDateFormat(this).format(new Date(timestamp))
    +" "+
    android.text.format.DateFormat.getTimeFormat(this).format(new Date(timestamp))

    +"\n"+

    android.text.format.DateUtils.formatDateTime(this,
                                                 timestamp,    
                                                 DateUtils.FORMAT_SHOW_DATE| 
                                                 DateUtils.FORMAT_SHOW_TIME|
                                                 DateUtils.FORMAT_SHOW_YEAR)
);

在文本视图中提供以下输出:

gives me the following output in the textview:

Apr 27,2014 5:47:18 PM
27/04/2014 17:47
April 27,2014, 17:47

没有给出所需的输出,使用上述首选项将是这样: / p>

None gives the desired output, which would be something like this using the aforementioned preferences:

27/04/2014 17:47:18 

我研究了 joda-time-android 库,但它似乎并没有满足我的需要(如果我输入错了,请纠正我)。

I've looked into the joda-time-android library as well, but it doesn't seem to do what I need (correct me if I'm wrong).

TL; DR:如何在Android上根据用户首选项格式化日期/时间?

TL;DR: How do you format date/time according to user preferences with seconds on Android?

推荐答案

使用 @我希望我能想到一个好的的建议,我提出了以下代码,这些代码使用语言环境和用户设置来格式化日期:

Using @I wish I could think of a good's suggestion, I made the following code that formats date using locale and user settings:

public static String timeDateStringFromTimestamp(Context applicationContext,long timestamp){
    String timeDate;
    String androidDateTime=android.text.format.DateFormat.getDateFormat(applicationContext).format(new Date(timestamp))+" "+
            android.text.format.DateFormat.getTimeFormat(applicationContext).format(new Date(timestamp));
    String javaDateTime = DateFormat.getDateTimeInstance().format(new Date(timestamp));
    String AmPm="";
    if(!Character.isDigit(androidDateTime.charAt(androidDateTime.length()-1))) {
        if(androidDateTime.contains(new SimpleDateFormat().getDateFormatSymbols().getAmPmStrings()[Calendar.AM])){
            AmPm=" "+new SimpleDateFormat().getDateFormatSymbols().getAmPmStrings()[Calendar.AM];
        }else{
            AmPm=" "+new SimpleDateFormat().getDateFormatSymbols().getAmPmStrings()[Calendar.PM];
        }
        androidDateTime=androidDateTime.replace(AmPm, "");
    }
    if(!Character.isDigit(javaDateTime.charAt(javaDateTime.length()-1))){
        javaDateTime=javaDateTime.replace(" "+new SimpleDateFormat().getDateFormatSymbols().getAmPmStrings()[Calendar.AM], "");
        javaDateTime=javaDateTime.replace(" "+new SimpleDateFormat().getDateFormatSymbols().getAmPmStrings()[Calendar.PM], "");
    }
    javaDateTime=javaDateTime.substring(javaDateTime.length()-3);
    timeDate=androidDateTime.concat(javaDateTime);
    return timeDate.concat(AmPm);
}

这篇关于格式化日期和时间取决于用户的语言环境带有几秒钟的偏好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆