Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 566679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:00:12+00:00 2026-05-13T13:00:12+00:00

I’m currently building my first app for Android. What I’m having trouble with is

  • 0

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() {
    @Override
    public boolean setViewValue(View view, Cursor cursor,
            int columnIndex) {
        if (view.getId() == R.id.text1) {
            ((TextView) view).setText(getDateFormatView().format(
                parseDatabaseDate(cursor.getString(columnIndex))));
            return true;
        } else if (view.getId() == R.id.text2) {
            ((TextView)view).setText(
                cursor.getString(columnIndex));
          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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:00:13+00:00Added an answer on May 13, 2026 at 1:00 pm

    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))));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.