Reputation: 4337
I just want the date to show up like so:
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?
Upvotes: 43
Views: 69355
Reputation: 6058
I use this solution which is a mix of the other answers and also includes time
You can customize the DateFormat.SHORT
parts to other constants to show more/different details
Short leads to this on my device: 17/07/2021 04:11
You can simply pass Calendar.getInstance()
to get the current time
fun formatDateTime(calendar: Calendar):String{
val formatter = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)
formatter.timeZone = calendar.timeZone
return formatter.format(calendar.time)
}
Upvotes: 0
Reputation: 1909
Even simpler answer:
SimpleDateFormat.getDateTimeInstance().format(cal.getTime())
Upvotes: 1
Reputation: 231
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)
Upvotes: 0
Reputation: 6635
This is actually a fairly subtle problem to get right, and I've not seen another answer here on SO that addresses both:
Calendar
's time zone (which means that it might be showing a different date than local)Locale
(which affects the "right" way to format dates)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:
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
.
Upvotes: 17
Reputation: 6665
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
).
See the Android documentation for SimpleDateFormat
for more information.
The format specification of SimpleDateFormat
is similar to that of PHP's date
function:
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.
Upvotes: 126
Reputation: 34765
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,
and so on...
Upvotes: 21