localization - Displaying dates in localized format on Android -


I am currently preparing my first app for Android. Anything that bothers me is a local display of storage and dates in relation to a SimpleCursorAdapter . I have a class that opens access to a SQLutedatabase with three tables. This class takes care of depositing all the dates in the ISO format ("YYA-MM-DD"). When the date values ​​are read from the database and displayed on the screen, I want to format them in the local format. The approach I have come here uses ViewBinder to formatting:

  adapter.setViewBinder (new SimpleCursorAdapter.ViewBinder () {@Override Public boolean setViewValue (see view, cursor cursor, int ColumnIndex) {if (view.getId () == R.id.text1) {((TextView)) .set text (getDateFormatView). Format (parseDatabaseDate (cursor. GetString (columnIndex));; Else if; (view.getId () == R.id.text2) {(see TextView) .set text (cursor.getString (columnIndex)); true;} Else {return false;}}});   

getDateFormatView () is read from a strings.xml with a pattern creates a SimpleDateFormat object is. Parsing date from database with parseDatabaseDate () a SimpleDateFormat that is created using a continuous pattern yyyy-MM-dd .

Although this code works just fine, I am thinking that there is a better way to do this. I like what I do not like:

  • Two SimpleDateFormat objects (used to parse one in parseDatabaseDate () Is the date string from SQLiteDatabase , another is used to format values)
  • A java.util.Date object is created Which is then made
  • Too many (in my opinion) boilerplate code

That's why I am asking: Is there a better way of displaying dates in local format?

If you want to leave the parsing, you can store the date as long as possible. . You can then create a new date object using only zeros parsing for long periods of time.

This is not directly related to your question, but: one thing you would like to consider using, is to get your date formats. Thus, you format your date / time in user's local settings rather than your own presets. It can also simplify your code, because you do not need to create your own simplicity format.

Keeping these two things in mind, you can get rid of the calls in your own way:

  (see TextView). Set Text (android.text) .format.DateFormat.getDateFormat (). Format (new date (cursor .getString (columnIndex))));  

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -