Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Date.toLocaleString() - without time elements?

I am using Date.toLocaleString() in my app to get a string representation of the date - I only want the date (ie. Apr 5, 2012) without the time elements, is this possible?

However I achieve this I need it to be multi language so '5/4/2012' is no good as US dates are MM/DD/YYYY rather than DD/MM/YYYY for the UK.

Thanks

like image 654
Mike Avatar asked Sep 08 '26 11:09

Mike


2 Answers

As mentioned in the documentation, this method is deprecated. Instead, you should use a DateFormat (such as SimpleDateFormat or the built-in ones) which makes it easy to specify date patterns.

For example:

// You can specify styles if you want
DateFormat format = DateFormat.getDateInstance();
// Set time zone information if you want.
Date date = ...;
String text = format.format(date);
like image 55
Jon Skeet Avatar answered Sep 12 '26 21:09

Jon Skeet


I would like to answer your question in the comment by quoting the Android documentation of DateFormat class:

Most callers should avoid supplying their own format strings to this class' format methods, and rely on the correctly localized ones supplied by the system. This class' factory methods return appropriately-localized DateFormat instances, suitable for both formatting and parsing dates. For the canonical documentation of format strings, see SimpleDateFormat.

like image 28
Mahdi Alkhatib Avatar answered Sep 12 '26 22:09

Mahdi Alkhatib