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 has these classes that attempt to do the above, but none use both the user preferences and display the seconds.
java.text.DateFormat
Doesn't use preferences set in Settings app
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
Doesn't use preferences set for showing date in Settings app and no way to display seconds.
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
None gives the desired output, which would be something like this using the aforementioned preferences:
27/04/2014 17:47:18
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: 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);
}