在 Android 上以本地化格式显示日期 [英] Displaying dates in localized format on Android

查看:16
本文介绍了在 Android 上以本地化格式显示日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在构建我的第一个 Android 应用.我遇到的问题是与 SimpleCursorAdapter 相关的日期的存储和本地化显示.我有一个类,它封装了对具有三个表的 SQLitedatabase 的访问.此类负责以 ISO 格式(yyyy-MM-dd")存储所有日期.当从数据库中读取日期值并显示在屏幕上时,我希望它们以本地化格式进行格式化.这是我想出的方法.它使用 ViewBinder 进行格式化:

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() 使用从 strings.xml 读取的模式创建一个 SimpleDateFormat 对象.parseDatabaseDate() 使用由常量模式 yyyy-MM-dd 构造的 SimpleDateFormat 解析数据库中的日期.

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:

  • 两个 SimpleDateFormat 对象(一个用于 parseDatabaseDate() 以便从 SQLiteDatabase 解析日期字符串,另一个用于格式化值)
  • 一个 java.util.Date 对象被创建然后立即丢弃
  • 相当多(在我看来)样板代码.
  • 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?

推荐答案

如果你想跳过解析,你可以将日期存储为 long .然后,您可以使用零解析的 long 创建一个新的 Date 对象.

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.

这与您的问题没有直接关系,但是:您可能要考虑使用的一件事是 android.text.format.DateFormat 用于获取您的日期格式化程序.通过这种方式,您可以将日期/时间格式化为用户的区域设置,而不是您自己的预设.它还可能使您的代码更简单,因为您不再需要创建自己的 SimpleDateFormat.

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))));

这篇关于在 Android 上以本地化格式显示日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆