【问题标题】:How to format Android date without year and display month and day according to local settings? [duplicate]如何根据本地设置格式化没有年份的Android日期并显示月份和日期? [复制]
【发布时间】:2012-09-15 12:04:01
【问题描述】:

可能重复:
Android get current time and date

从这个Format date without year我知道如何摆脱年。

但我也想形成月份和日期。 如果用户设置 09/24/2012,则显示 09/24; 如果用户设置 24/09/2012,则显示 24/09。

我也想支持l18n。

也许解决方案是结合 DateFormat 和 DateUtils,但我不知道该怎么做。

【问题讨论】:

  • stackoverflow.com/questions/5369682/… 你调查过这个吗?
  • 也许这不是我想要的。实际上我有很长的时间想要格式化它并使用 int TextView.setText()。找到了一个函数DateFormat.getDateFormatOrder(),但是不知道怎么用。
  • 如果问题是是否显示 M/D 而不是 D/M,就做 D/M,大多数国家使用 D/M/Y
  • 这是我的“区域感知”解决方案 DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.LONG); // 根据需要使用 MEDIUM 或 SHORT String date = dateFormatter.format(now); // 删除年份 String year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR)); date = date.replace(year, "").trim();将以法语输出“13 décembre”:-)
  • 有趣的事实:这个问题真的不是重复的。是的,我们都知道如何获取特定格式的日期。这不是这个问题要问的。看起来stackoverflow.com/questions/3790918/format-date-without-year 确实可以工作,因为 android.text.format.DateFormat 使用 SimpleDateFormats。不过,这仍然很难看。

标签: android date-format


【解决方案1】:

首先将信息拆分为变量,然后根据需要显示。

public String formatDate(String date)
{
    int index1, index2;

    index1 = date.indexOf('/');
    index2 = date.lastIndexOf('/');

    String str1, str2, str3;

    str1 = date.substring(0, index1);
    str2 = date.substring(index1 + 1, index2);
    str3 = date.substring(index2);

    // Presuming that str1 is the month and str2 is the day ...

    return str1 + "/" + str2;
}

【讨论】:

  • 这将在语言环境设置为非美国格式(如 24.10.1998 等)的所有设备上失败。
  • 我假设使用此代码的人足够聪明,可以知道这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-22
  • 1970-01-01
  • 1970-01-01
  • 2014-12-20
  • 1970-01-01
  • 2020-10-18
  • 1970-01-01
相关资源
最近更新 更多