【问题标题】:How to change simpledateformat according to device/local timezone如何根据设备/本地时区更改 simpledateformat
【发布时间】:2014-06-21 11:08:53
【问题描述】:

我的代码:

SimpleDateFormat today= new SimpleDateFormat("E, MMM dd, yyyy");
 TimeZone tz = TimeZone.getDefault();
 today.setTimeZone(tz);
 dt = today.format(c.getTimeInMillis());
 time.setText("Today "+dt);

我的默认日期格式是 2014 年 5 月 5 日星期一

如果我将设备中的设置更改为格式:2014/12/31 那么我应该以 Mon, 2014, May 05 这样的格式获取我的日期 有没有可能,如果是的话怎么......?

【问题讨论】:

  • 可能是 this SO-post 的副本。使用Settings.System.DATE_FORMAT 的公认答案是有希望的。
  • yes..same..but still..my dbt is we can set that date format to Mon, 2014, May 05 如果我选择我的设置日期格式为 2014/12/11

标签: android timezone simpledateformat


【解决方案1】:

不要以这种方式使用 SimpleDateFormat 来解决您的问题。 使用这个:

DateFormat df = android.text.format.DateFormat.getTimeFormat(context);

这将为您带来手机设置的日期格式

然后将您输入的日期放入:

DateUtils.format(...)

使用df 作为模式。

【讨论】:

  • 这就是你今天的意思 DateFormat = android.text.format.DateFormat.getTimeFormat(getActivity()); DateUtils.formatDate(df, "E, MMM dd, yyyy");但它不以那种格式显示..它只显示时间
  • 它仍然以 Mon, May 05, 2014 的格式显示
【解决方案2】:

您可以使用DateUtils.formatDateTime(...) 方法根据当地惯例获取格式化的日期或时间

【讨论】:

  • dt = DateUtils.formatDate(df,"E, MMM dd, yyyy");像这样??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-15
  • 1970-01-01
相关资源
最近更新 更多