Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting a calendar date

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?

like image 483
scarhand Avatar asked May 27 '12 02:05

scarhand


People also ask

What is the default formatting for dates?

The default date format shows as YYYY-MM-DD.

How do you enter a date in mm dd yyyy?

yyyy-MM-dd — Example: 2013-06-23.

How do you write a date range correctly?

Ranges. A simple year–year range is written using an en dash ( – , – or {{ndash}} ), not an em dash, hyphen, or slash; this dash is unspaced (that is, with no space on either side); and the end year is usually given in full: 1881–1882; 1881–1886 (not 1881–86); 1881–1992 (not 1881–92)


1 Answers

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.

like image 92
creemama Avatar answered Sep 21 '22 06:09

creemama