我只希望日期显示如下:
2012 年 5 月 26 日星期六晚上 10:42
到目前为止,这是我的代码:
Calendar calendar = Calendar.getInstance();
String theDate = calendar.get(Calendar.MONTH) + " " + calendar.get(Calendar.DAY_OF_MONTH) + " " + calendar.get(Calendar.YEAR);
lastclick.setText(getString(R.string.lastclick) + " " + theDate);
这显示了月、日和年的数字,但必须有更好的方法吗?是不是有一些简单的方法,比如使用 PHP 的 date()
函数?
最佳答案
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM d, yyyy 'at' h:mm a");
System.out.println(format.format(calendar.getTime()));
运行上述代码会输出当前时间(例如,2012 年 5 月 26 日星期六晚上 11:03
)。
见 Android documentation for SimpleDateFormat
了解更多信息。
SimpleDateFormat
的格式规范类似于PHP的date
功能:
echo date("l, M j, Y \a\\t g:i A");
你是对的。相比Java代码,PHP代码简洁很多。
https://stackoverflow.com/questions/10771279/
相关文章:
github - 如何在 GitHub 上的 MD markdown 自述文件中添加空格?
.net - 什么是 String.Format 的 WPF XAML 数据绑定(bind)等效项?
php - 当我将我的数字指定为 .000021 时,为什么 PHP 会以科学计数法打印我的数字?
ruby-on-rails - 在 rails3 中禁用 :. 格式路由
python - 如何在 Python/Pandas 中将变量设置为 "Today' s"date
html - 每当我调整编辑器窗口的大小时,如何阻止代码行自动转移到下一行?