使用 SimpleDate 的 Android 日期格式

标签 android simpledateformat

我正在为 Android 开发一个包含十二个不同项目的 XML 解析器,我需要帮助为每个项目创建一个日期。这是我目前所拥有的:

TextView detailsPubdate = (TextView)findViewById(R.id.detailspubdate);

我希望日期看起来像这样:9 月 3 日,星期六。谢谢您的帮助!

最佳答案

如果您要查找今天的日期,可以使用 Date 对象来实现。

Date today = new Date();//this creates a date representing this instance in time.

然后将日期传递给 SimpleDateFormat.format(Date) 方法。

// "EEEE, MMMM, d" is the pattern to use to get your desired formatted date.
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, MMMM, d");
String formattedDate = sdf.format(today);

最后,您可以将此 String 设置到您的 TextView

detailsPubdate.setText(formattedDate);

这是 SimpleDateFormat 的文档.该文档显示了可用于格式化 Date 对象的各种模式。

关于使用 SimpleDate 的 Android 日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7285599/

相关文章:

android - CHOICE_MODE_MULTIPLE 和 CHOICE_MODE_MULTIPLE_MODAL 的区别?

java.text.ParseException : Unparseable date "yyyy-MM-dd' T'HH:mm:ss. SSSZ"- SimpleDateFormat

java - SimpleDateFormat 无法解析的日期异常

java - SimpleDateParser 产生不正确的日期?

java - 无法从 QuickAction 打开 AlertDialog

android - 使用 SearchView 过滤包含 LiveData 内容的 RecyclerView 列表

android - 未收到通知的 PendingIntent

android 将位图正确保存为 jpeg 但不显示在图片库中

android - 将UTC转换为伊朗本地时间

java - 日期时区转换