【问题标题】:Android Date.toLocaleString() - without time elements?Android Date.toLocaleString() - 没有时间元素?
【发布时间】:2011-12-02 12:25:05
【问题描述】:

我在我的应用程序中使用 Date.toLocaleString() 来获取日期的字符串表示形式 - 我只想要没有时间元素的日期(即 2012 年 4 月 5 日),这可能吗?

但是我实现了这一点,我需要它是多语言的,所以“2012 年 5 月 4 日”并不好,因为美国的日期是 MM/DD/YYYY,而不是英国的 DD/MM/YYYY。

谢谢

【问题讨论】:

  • 谢谢乔恩,原谅我的无知,但是我如何从设备中获取时区信息并使用它来使用您的方法格式化日期?

标签: android datetime


【解决方案1】:

作为mentioned in the documentation,此方法已被弃用。相反,您应该使用DateFormat(例如SimpleDateFormat 或内置的),这样可以轻松指定日期模式。

例如:

// You can specify styles if you want
DateFormat format = DateFormat.getDateInstance();
// Set time zone information if you want.
Date date = ...;
String text = format.format(date);

【讨论】:

    【解决方案2】:

    我想在评论中引用the Android documentation of DateFormat class来回答你的问题:

    大多数调用者应避免为此提供自己的格式字符串 类的格式方法,并依赖于正确本地化的方法 由系统提供。 这个类的工厂方法返回 适当本地化的 DateFormat 实例,适用于两者 格式化和解析日期。对于规范文档 格式字符串,请参阅 SimpleDateFormat。

    【讨论】:

      猜你喜欢
      • 2019-02-22
      • 1970-01-01
      • 2013-07-02
      • 1970-01-01
      • 2018-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多