Java Date & Time Format (JDTF)

I often find cases of date & time in several Android projects, both academy projects and corporate projects. In the past, when I first solved this case, I often asked about this on stackoverflow and there were many discussions that were identical to,

"How to set the date & time format correctly according to the configuration of the mobile device used, arranged into year, month, day, hour and minute?" 

The solution is to use the standard Java DateFormat class.

One example of implementing DateFormat is shown in the code below.

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

You can initialize a Date object with your own values, but you should be aware that the constructor is deprecated and you should really be using the Java Calendar object.

You can use DateFormat. The result depends on the phone's default Locale, but you can also specify the Locale, by following  the Documentation instructions , you can apply some examples of the following formats:

Example 1

DateFormat.getDateInstance().format(date)

FR Locale : 3 Nov. 2017

US/En Locale : Jan 12, 1952

Example 2

DateFormat.getDateInstance(DateFormat.SHORT).format(date)

FR Local : 11/03/2017

US/En Locale : 12.13.52

Example 3

DateFormat.getDateInstance(DateFormat.MEDIUM).format(date)

FR Locale : 3 Nov. 2017

US/En Locale : Jan 12, 1952

Example 4

DateFormat.getDateInstance(DateFormat.LONG).format(date)

FR Locale : November 3, 2017

US/En Locale : January 12, 1952

Example 5

DateFormat.getDateInstance(DateFormat.FULL).format(date)

FR Locale : vendredi 3 novembre 2017

US/En Locale : Tuesday, April 12, 1952

Example 6

DateFormat.getDateTimeInstance().format(date)

DateFormat.getDateTimeInstance().format(date)

Example 7

DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(date)

FR Locale : 11/03/2017 16:04

Example 8

DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(date)

FR Locale : 11/03/2017 16:04:58

Example 9

DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG).format(date)

FR Locale : 11/03/2017 16:04:58 GMT+01:00

Example 10

DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL).format(date)

FR Locale : 11/03/2017 16:04:58 heure normale d'Europe centrale

Example 11

DateFormat.getTimeInstance().format(date)

FR Locale : 16:04:58

Example 12

DateFormat.getTimeInstance(DateFormat.SHORT).format(date)

FR Local : 16:04

Example 13

DateFormat.getTimeInstance(DateFormat.MEDIUM).format(date)

Example 14

DateFormat.getTimeInstance(DateFormat.LONG).format(date)

FR Locale : 16:04:58 GMT+01:00

Example 15

DateFormat.getTimeInstance(DateFormat.FULL).format(date)

FR Locale : 16:04:58 heure normale d'Europe centrale

Appendix: 3rd Party Library

A simple date and time formatting library for Android  https://redplay.github.io/dates/

Post a Comment

Previous Next

نموذج الاتصال