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);
April 7, 2014 @ 10:10
I think two more things are worth mentioning:
1. There is the class DateUtils in Android’s SDK which helps you formatting date Strings: http://developer.android.com/reference/android/text/format/DateUtils.html
2. There is a Android specific port of Joda-Time by Daniel Lew: https://github.com/dlew/joda-time-android
I strongly recommend to use Joda-Time whenever you have to deal with date operations. It not only is consistent (no days starting with “0” but months starting with “1”) it also offers plenty of operations to find out the last day of the month, go back x days and so forth. Daniel Lew’s port is necessary because of a memory issue with the stock joda-time library. An issue specific to Android only!
BTW: Daniel is working on porting DateUtils to the joda-time library as well. So I recommend to use his version since the stock Android version is not really perfect.
April 8, 2014 @ 07:15
1. The DateUtils class looks very nice for formatting date and time spans, I was not aware it existed.
2. I’m not against using the Joda-Time library, but one of the requirements for my products is to use as few external dependancies as possible. I agree that the built-it date operations are not exactly easy with the standard Java classes, and that Joda-Time looks like a good alternative when it can be used.
June 4, 2014 @ 08:53
How to get date format of specific country without use of locale
June 5, 2014 @ 19:49
To get the date format for a country you need the locale, which includes the country and the language. In some countries, there is more than one date format depending on the language, so just the country is not enough.
When you get the instance of the Calendar object, you can specify a locale. For example:
Calendar currentDate = Calendar.getInstance(Locale.CANADA_FRENCH);
October 31, 2014 @ 11:10
Thank you so much for this post. It was very helpful.
October 24, 2016 @ 09:04
How to use the am/pm in date by using symbol??Thanks in advance
October 29, 2016 @ 10:17
With java.text.SimpleDateFormat (that can also format times since the Date class also includes the time), the “a” symbol is used in the pattern for the AM/PM marker.