Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 java.text.DateFormat object that formats output correctly using getDateFormat() and getTimeFormat(), but getTimeFormat() 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();

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?

like image 336
plusCubed Avatar asked Apr 28 '14 02:04

plusCubed


People also ask

What is the correct format for dates?

The international standard recommends writing the date as year, then month, then the day: YYYY-MM-DD. So if both Australians and Americans used this, they would both write the date as 2019-02-03. Writing the date this way avoids confusion by placing the year first.

How do I format a date in DD MMM YYYY in Excel?

First, pick the cells that contain dates, then right-click and select Format Cells. Select Custom in the Number Tab, then type 'dd-mmm-yyyy' in the Type text box, then click okay. It will format the dates you specify.

Which countries use date format mm dd yyyy?

According to wikipedia, the only countries that use the MM/DD/YYYY system are the US, the Philippines, Palau, Canada, and Micronesia. I would presume these other countries, being close to or influenced by the US, have picked it up from the US, indicating that it is a US creation.


2 Answers

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);
}
like image 188
plusCubed Avatar answered Sep 27 '22 20:09

plusCubed


Try this:

long timestamp = System.currentTimeMillis();

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

String currentTimeString =  dateFormat.format(timestamp) + " - " +            
       timeFormat.format(timestamp);
like image 37
Ingo Avatar answered Sep 27 '22 20:09

Ingo