When I was looking how to convert date and time to text in Android, I found method Date.toLocaleString(). This method is deprecated, and we should use DateFormat class.
The most important methods of DateFormat class are getDateTimeInstace(), getDateInstance() and getTimeInstance(). You need to call one of this methods in order to get correct DateTime class instance for converting dates and times to strings.
If you call any of this methods without parameters you will get formater which will convert your date or time to default string format for the user's default locale.
Important:
It is important that you use java.text.DateFormat and not android.text.format.DateFormat, so check your imports if you have any errors.
Some examples
To convert date and time to default format:
Date dt =
new
Date();
DateFormat formater = DateFormat.getDateTimeInstance();
String formated = formater.format(dt);
To convert date and time to specific format:
Date dt =
new
Date();
DateFormat formater = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT);
String formated = formater.format(dt);
For more information about DateFormat class see
here