plu*_*bed 7 java time android date android-dateutils
我试图根据用户的设置获取格式化的日期(年、月、日期)和时间(小时、分钟、秒)字符串。Android Developers Google Group 中的这篇文章描述了我遇到的确切问题,但没有人帮助该人解决问题。我总结一下:
Android 有这些类尝试执行上述操作,但没有一个类同时使用用户首选项和显示秒数。
java.text.DateFormat
不使用设置应用程序中设置的首选项
android.text.format.DateFormat
获取一个java.text.DateFormat
使用getDateFormat()
and正确格式化输出的对象getTimeFormat()
,但getTimeFormat()
不包括秒。
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)
);
Run Code Online (Sandbox Code Playgroud)
在文本视图中给我以下输出:
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)
);
Run Code Online (Sandbox Code Playgroud)
None 提供所需的输出,使用上述首选项将是这样的:
Apr 27,2014 5:47:18 PM
27/04/2014 17:47
April 27,2014, 17:47
Run Code Online (Sandbox Code Playgroud)
我也研究了joda-time-android库,但它似乎没有做我需要的(如果我错了,请纠正我)。
TL;DR:你如何根据用户的喜好在 Android 上用秒来格式化日期/时间?
根据@I wish I could think of a good
的建议,我编写了以下代码,使用区域设置和用户设置格式化日期:
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);
}
Run Code Online (Sandbox Code Playgroud)