Android 格式化日历日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10771279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Formatting a calendar date
提问by scarhand
I just want the date to show up like so:
我只希望日期像这样显示:
Saturday, May 26, 2012 at 10:42 PM
Saturday, May 26, 2012 at 10:42 PM
Here's my code so far:
到目前为止,这是我的代码:
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);
This shows the numbers of the month, day, and year, but there's got to be a better way of doing this? Isn't there some simple way of doing this like using PHP's date()
function?
这显示了月、日和年的数字,但必须有更好的方法来做到这一点?是不是有一些简单的方法可以做到这一点,比如使用 PHP 的date()
函数?
采纳答案by Shankar Agarwal
Use the below to format the date as required. Refer this LINK
使用以下内容根据需要格式化日期。参考这个链接
Calendar calendar = Calendar.getInstance();
lastclick.setText(getString(R.string.lastclick) + " " + String.format("%1$tA %1$tb %1$td %1$tY at %1$tI:%1$tM %1$Tp", calendar));
Where %1$tA for staurday, %1$tb for May,
其中 %1$tA 为 staurday,%1$tb 为五月,
and so on...
等等...
回答by creemama
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM d, yyyy 'at' h:mm a");
System.out.println(format.format(calendar.getTime()));
Running the above code outputs the current time (e.g., Saturday, May 26, 2012 at 11:03 PM
).
运行上面的代码输出当前时间(例如,Saturday, May 26, 2012 at 11:03 PM
)。
See the Android documentation for SimpleDateFormat
for more information.
有关更多信息,请参阅Android 文档SimpleDateFormat
。
The format specification of SimpleDateFormat
is similar to that of PHP's date
function:
的格式规范SimpleDateFormat
类似于PHPdate
函数的格式规范:
echo date("l, M j, Y \a\t g:i A");
You're right. Compared to the Java code, the PHP code is much more succinct.
你是对的。与Java代码相比,PHP代码简洁得多。
回答by String
This is actually a fairly subtle problem to get right, and I've not seen another answer here on SO that addresses both:
这实际上是一个相当微妙的问题,我还没有在 SO 上看到另一个解决这两个问题的答案:
- The
Calendar
's time zone (which means that it might be showing a different date than local) - The device's
Locale
(which affects the "right" way to format dates)
- 所述
Calendar
的时区(这意味着其可能呈现出不同的日期比本地) - 设备的
Locale
(影响格式化日期的“正确”方式)
The previous answers to this question ignore locale, and other answers that involve conversion to a Date
ignore the time zone. So here's a more complete, general solution:
此问题的先前答案忽略区域设置,以及其他涉及转换为Date
忽略时区的答案。所以这里有一个更完整、更通用的解决方案:
Calendar cal = Calendar.getInstance(); // the value to be formatted
java.text.DateFormat formatter = java.text.DateFormat.getDateInstance(
java.text.DateFormat.LONG); // one of SHORT, MEDIUM, LONG, FULL, or DEFAULT
formatter.setTimeZone(cal.getTimeZone());
String formatted = formatter.format(cal.getTime());
Note that you need to use java.text.DateFormat
here, not Android's own (confusingly-named) android.text.format.DateFormat
.
请注意,您需要在java.text.DateFormat
此处使用,而不是 Android 自己的 (令人困惑的命名) android.text.format.DateFormat
。
回答by D.Roters
For me this works perfectly:
对我来说,这非常有效:
val cal = Calendar.getInstance()
val pattern = "EEEE, MMMM d, yyyy 'at' h:mm a"
val primaryLocale = getLocales(resources.configuration).get(0)
val dateFormat = SimpleDateFormat(dateFormatPattern, primaryLocale)
val formatedDate: String = dateFormat.format(cal.time)
回答by Arutyun Enfendzhyan
Even simpler answer:
更简单的答案:
SimpleDateFormat.getDateTimeInstance().format(cal.time)