Displaying dates in localized format on Android

I'm currently building my first app for Android. What I'm having trouble with is the storage and localized display of dates in connection with a SimpleCursorAdapter. I have a class that encapsulates access to an SQLitedatabase with three tables. This class takes care of storing all dates in ISO format ("yyyy-MM-dd"). When the date values are read from the database and displayed on the screen I want them to be formatted in a localized format. Here's the approach I've come up with. It uses a ViewBinder to do the formatting:

adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
    public boolean setViewValue(View view, Cursor cursor,
            int columnIndex) {
        if (view.getId() == R.id.text1) {
            ((TextView) view).setText(getDateFormatView().format(
            return true;
        } else if (view.getId() == R.id.text2) {
          return true;
        } else {
            return false;

getDateFormatView() creates a SimpleDateFormat object with a pattern that is read from strings.xml. parseDatabaseDate() does the parsing of the date from the database with a SimpleDateFormat that is constructed using a constant pattern yyyy-MM-dd.

Although this code works just fine I'm wondering if there is a better way to do that. What I don't like is that I need:

  • two SimpleDateFormat objects (one is used in parseDatabaseDate() in order to parse the date string from the SQLiteDatabase, the other is used to format the value)
  • a java.util.Date object that is created then thrown away immediately
  • quite a lot of (in my opinion) boilerplate code.

So that's why I'm asking: is there a better way to display dates in localized format?

Asked by: Sophia944 | Posted: 20-01-2022

Answer 1

If you want to skip on the parsing, you could store the date as a long instead. Then you could just create a new Date object using the long with zero parsing.

This isn't directly related to your question, but: One thing you might want to consider using is android.text.format.DateFormat for getting your date formatters. This way you format your dates/times to the user's locale settings instead of your own presets. It might also make your code simpler, as you don't need to create your own SimpleDateFormat anymore.

With these two things in mind, you can get rid of the calls to your own methods:

((TextView) view).setText(android.text.format.DateFormat.getDateFormat().format(new Date(cursor.getString(columnIndex))));

Answered by: Kristian209 | Posted: 21-02-2022

Similar questions

localization - Are there any tools to convert an Iphone localized string file to a string resources file that can be used in Android?

Closed. This question does not meet Stack Overflow guid...

localization - How to set different locales in android?

In my application, I need to display strings according to user locale selection. So, I put my strings.xml in values-en, values-ko, etc. How can I set locale us, australia i.e; values-en_US, values-en_AU? But it's throwing an error? Can any one tell me how to set these locales in my code?

android - Localization of Facebook

I am working on an Android game.. I live in Taiwan but i want to make an English game, i have some problem when using facebook login Dialog is always loaded in Chinese. Is there any way i can change it to English which is the same with the device's language? Thanks, Eve

localization - Custom locale in Android

the Android phones in Slovakia have a custom locale - en_SK and cs_SK. Language is either Czech or English, location is Slovakia. Now I would like to have strings that target these locales. The default approach is to create a folder "values-CountryCode", for Czech Republic it's values-cs. But I get a build error if I add a folder name values-en_SK or values-cs_SK: "invalid resource directory name" What to ...

What's the android intent to show up the localization settings?

I'm sure this can be answered really easily. I just do not find the intent id for this job. I want to show up that localization settings page, where the user can select "turn on localization through wifi..." Intent intent = new Intent( ??? ); startActivity(intent);

localization - How to let android device "speak" Chinese?

I'm learning to develop app on Android with eclipse and ADT plug-in. Now I want my app to read Chinese text, according to this artical in android SDK document http://androidappdocs.appspot.com/resources/articles/tts.html The TTS engine support speaking English, French, German, Italian and Spanish, so ...

localization - Localized week days on android

I'm trying to implement something like scheduling in my application, so I need to get names of all the day in a week and show it to user. It is simple: i can just make an enum with all the days and use it, but the localization problem shows up. The list will have english lables always. Is there a way to localize the days of week simply? Thanks.

How can I use msgid during Android strings localization?

In Android sources I see such strings: <string name="app_name" msgid="8095131950334945205">"Calculadora"</string> Notice the 'msgid', this indicates existence of some tool to manage strings translation. What the tool is it?

android - Localization and drawables

I want to localize an image by adding the folder res/drawable-hdpi-no, but I get an error stating "invalid resource directory name". What's up with this?

localization - Android emulator does not display Hindi font

I changed the locale to hi_IN on the emulator. When I start my application (or even the home screen) on the emulator, all I see is boxes reminiscent of "i dont know how to render this font". Any input on why this might be happening and how to get rid of this is most welcome. I need to display the hindi font in order to test localization of my application. Thanks, Sriram.

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android