Formatting Dates with Java in Android Applications

To format dates in an Android application, you must keep in mind that dates formatted using the Android SDK take into account the locale, which includes the country and language (this is also called a culture). The locale is configured in the Settings application of the device. In general using the locale of the device is the best option, but this may be unacceptable for enterprise applications where all users need to see the same format, regardless of the language of the device. Also, dates that are saved to a file or a database should always have the same format.

Here is a list of the many classes involved in handling and formatting dates :

  • java.util.Date : represents a single date and compares dates. In Java, the date object also includes the time, so if you only need the date the time should always be the same (for example midnight) to make sure comparisons will return the expected result.
  • java.util.Calendar : extracts the data for the day, month and year from dates and handles mathematical operations between dates.
  • android.text.format.DateFormat : gets the date and time format according the current locale of the device. The format is returned as a java.text.Format that can be used with the Java format classes.
  • java.text.DateFormat : represents a date format. It should not be used directly since it does not manage the Android locale.
  • java.text.SimpleDateFormat : derived from java.text.DateFormat, format a date according to the specified format.
  • java.sql.Date/java.sql.Time : two classes derived from the java.util.Date class used to handle dates in the SQL format to write to a database or read from it. Those classes are used to split the java.util.Date into a SQL Date and SQL Time. They will not be used in this article, but since they derive from java.util.Date, everything that works with a java.util.Date will work with a java.sql.Date or a java.sql.Time.

The java.util.Date is a specific point on a timeline : it can check if its date is before or after another date, but it has no idea of how it fits in a month or in a year. The java.util.Calendar class is the one that handles how a calendar for a year behaves: for example, you need a Calendar object to get the current date or time, to know when a month start and ends or to check what is the next year. So, to get a java.util.Date for the current date, you must get an instance of the java.util.Calendar, which is initialised by default at the current date and time.

Date currentDate = Calendar.getInstance().getTime();

After that, if you want to display the date retrieved from the calendar, you need to get the date format for the current locale of the device with the android.text.format.DateFomat class that returns a java.text.DateFormat. For example, to display the current date and time in the current locale, you can use the following code :

Date currentDate = Calendar.getInstance().getTime();
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(this);
String formattedCurrentDate = dateFormat.format(currentDate);

Finally, if you need to format a date using a fixed format, you can supply your own format to the java.util.SimpleDateFormat class :

Date currentDate = Calendar.getInstance().getTime();
java.text.SimpleDateFormat simpleDateFormat = new java.text.SimpleDateFormat("dd/MM/yyyy");
String formattedCurrentDate = simpleDateFormat.format(currentDate);