7

我正在尝试根据用户的设置获取格式化的日期(年、月、日)和时间(小时、分钟、秒)字符串。Android Developers Google Group 中的这篇文章描述了我遇到的确切问题,但没有人帮助那个人解决它。我总结一下:

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

  1. java.text.DateFormat
    不使用设置应用程序中设置的首选项

  2. android.text.format.DateFormat
    获取一个使用andjava.text.DateFormat正确格式化输出的对象,但不包括秒数。getDateFormat()getTimeFormat()getTimeFormat()

  3. android.text.format.DateUtils
    不使用在设置应用程序中显示日期的首选项设置,也无法显示秒数。

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

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)
);

在 textview 中给我以下输出:

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

None 给出了所需的输出,使用上述首选项将是这样的:

27/04/2014 17:47:18 

我也查看了joda-time-android库,但它似乎没有做我需要的(如果我错了,请纠正我)。

TL;DR:您如何根据用户偏好在 Android 上使用秒来格式化日期/时间?

4

5 回答 5

4

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);
}
于 2014-05-03T00:28:23.603 回答
4

尝试这个:

long timestamp = System.currentTimeMillis();

DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
DateFormat timeFormat = DateFormat.getTimeInstance();

String currentTimeString =  dateFormat.format(timestamp) + " - " +            
       timeFormat.format(timestamp);
于 2014-10-23T22:42:01.310 回答
3

为什么不使用混合解决方案

android.text.format.DateFormat.getDateFormat(this).format(new Date(timestamp));

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

将其保存为getDateTimeInstance字符串并提取秒数,然后将其附加到getDateFormat

于 2014-04-28T02:19:46.987 回答
1

有点破解,但这似乎工作:

Date date = ...;
String dateStr = DateFormat.getLongDateFormat(context).format(date);
String timeStr;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
  String timeFormat = DateFormat.getBestDateTimePattern(Locale.getDefault(), DateFormat.is24HourFormat(context) ? "Hms" : "hmsa");
  timeStr = new SimpleDateFormat(timeFormat, Locale.getDefault()).format(date);
}
else {
  String timeFormat = DateFormat_getTimeFormatString(context).replace("mm", "mm:ss");
  timeStr = new SimpleDateFormat(timeFormat, Locale.getDefault()).format(date);
}
return dateStr + " " + timeStr;

它依赖于 `DateFormat˙ 中的一个隐藏函数,我们可以通过反射使用它:

private static String DateFormat_getTimeFormatString(Context context) {
  try {
    final Method method = DateFormat.class.getDeclaredMethod("getTimeFormatString");
    method.setAccessible(true);
    return (String) method.invoke(context);
  }
  catch (Exception ignored) {
    return "HH:mm";
  }
}

从 API 18 开始,您可以使用新getBestDateTimePattern()功能,该功能只需要您想要显示的数据列表并返回最佳匹配模式。

如果您查看is24HourFormat()源代码,这有点疯狂(它检查用户首选模式字符串中是否存在大写H),因此如果您需要显示许多日期,可能值得存储它的值而不是重复调用它.

当然,您可以通过调用其他实例或使用DateUtils.formatDateTime().

不过,有一个警告。确保您使用活动上下文来请求is24HourFormat(). 应用程序上下文不会。

于 2016-01-05T14:06:24.380 回答
0
于 2014-04-28T06:12:12.263 回答