Wednesday, 24 January 2018

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?


Answer



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.


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...