plusCubed plusCubed - 4 years ago 271
Java Question

Formatting date & time according to user's locale & preferences with seconds

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.

  1. java.text.DateFormat

    Doesn't use preferences set in Settings app

  2. android.text.format.DateFormat

    Gets a
    object that formats output correctly using
    , but
    doesn't include seconds.

  3. 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();

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


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



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?

Answer Source

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];
            AmPm=" "+new SimpleDateFormat().getDateFormatSymbols().getAmPmStrings()[Calendar.PM];
        androidDateTime=androidDateTime.replace(AmPm, "");
        javaDateTime=javaDateTime.replace(" "+new SimpleDateFormat().getDateFormatSymbols().getAmPmStrings()[Calendar.AM], "");
        javaDateTime=javaDateTime.replace(" "+new SimpleDateFormat().getDateFormatSymbols().getAmPmStrings()[Calendar.PM], "");
    return timeDate.concat(AmPm);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download