Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format date and time in Android?

How to format correctly according to the device configuration date and time when having a year, month, day, hour and minute?

like image 444
pupeno Avatar asked Jan 17 '09 23:01

pupeno


People also ask

What is date format in Android Studio?

DateFormat is an abstract class for date/time formatting subclasses which formats and parses dates or time in a language-independent manner. The date/time formatting subclass, such as SimpleDateFormat , allows for formatting (i.e., date → text), parsing (text → date), and normalization.

How can I get current date format in Android?

You can use the SimpleDateFormat class for formatting date in your desired format. Just check this link where you get an idea for your example. For example: String dateStr = "04/05/2010"; SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy"); Date dateObj = curFormater.


1 Answers

Use the standard Java DateFormat class.

For example to display the current date and time do the following:

Date date = new Date(location.getTime()); DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext()); mTimeText.setText("Time: " + dateFormat.format(date)); 

You can initialise a Date object with your own values, however you should be aware that the constructors have been deprecated and you should really be using a Java Calendar object.

like image 174
JamieH Avatar answered Oct 09 '22 22:10

JamieH