DateTime is a common utility in Android. These days most of the project required date and time formatting in Android. So we are building a plug and play solution. I hope each developer will keep this utility in own inbox for time-saving purpose. In this tutorial, we demonstrate different-different date formatting and parsing with the help of Calendar and SimpleDateFormat classes in Android using Java.

Calendar Class

The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week. An instant in time can be represented by a millisecond value that is an offset from the Epoch, January 1, 1970 00:00:00.000 GMT (Gregorian).

Calendar rightNow = Calendar.getInstance();

Like other locale-sensitive classes, Calendar provides this class method, getInstance, for getting a generally useful object of this type. Calendar’s getInstance method returns a Calendar object whose calendar fields have been initialized with the current date and time.

SimpleDateFormat Class

SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date → text), parsing (text → date), and normalization.

SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting. However, you are encouraged to create a date-time formatter with either getTimeInstance, getDateInstance, or getDateTimeInstance in DateFormat. Each of these class methods can return a date/time formatter initialized with a default format pattern.

Using Calendar object in SimpleDateFormat

As you might already understood that Calendar’s getInstance method returns a Calendar object with the current date and time. Now in order to display the DateTime according to your own formatting you’ll have the use this calendar object with the SimpleDateFormat. Let’s check a demonstration code.

Calendar rightNow = Calendar.getInstance();

SimpleDateFormat formatter = new SimpleDateFormat("EE, dd/MM/yyyy");
String today = formatter.format(rightNow.getTime());

If you show the output of today inside a TextView or Toast then you will see something similar to Sun, 31/05/2020 as while I am writing this tutorial it’s May 31 of 2020.

You may notice that I have passed a pattern (EEE, dd/MM/yyyy) as String to format how I want the output. E represents the Day name in week, d is the Day in month, M is the Month in year and y is the Year. You may use lots of other formats also. I made a chart for you which may help you to find your desired DateTime formats.

Sr. No.DateTime FormatResulting Value
01dSingle digit date. e.g. 5
02ddDouble digit date. e.g. 05
03EEThree-letter abbreviation of day name in week. e.g. Mon
04EEEEDay name in week spelled out in full. e.g. Monday
05uSingle digit day number of week (1 = Monday … 7 = Sunday). e.g. 1
06uuDouble digit day number of week (01 = Monday … 07 = Sunday). e.g. 01
07MSingle digit month. e.g. 1
08MMDouble digit month. e.g. 01
09MMMThree-letter abbreviation of month. e.g. Jan
10MMMMMonth spelled out in full. e.g. January
11yyDouble digit year. e.g. 20
12yyyyFour digit year. e.g. 2020
13hSingle digit hour in 12 hour format. e.g. 9
14hhDouble digit hour in 12 hour format. e.g. 09
15HSingle digit hour in 24 hour format. e.g. 21 for 9 PM
16HHDouble digit hour in 24 hour format. e.g. 21 for 9 PM
17mSingle digit minute. e.g. 5
18mmDouble digit minute. e.g. 05
19sSingle digit second. e.g. 7
20ssDouble digit second. e.g. 07
21aam/pm marker. e.g. PM

You can use any of the format to parse the Calendar object according to your choice in SimpleDateFormat. You can find more patterns in official Android Developers Docs.

Format Tomorrow’s Date

The Calendar object supports various modification. When we initialized the Calendar object, it automatically selects the current Date & Time but then we can add 1 more day with it to select the date of Tomorrow or day after tomorrow and so on.

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, 1);

SimpleDateFormat formatter = new SimpleDateFormat("EEEE, dd/MM/yyyy");
String tomorrow = formatter.format(calendar.getTime());

Using the add() method we increased the day of year by 1. If you increase by 2 then it will select the day after tomorrow.

Parse Custom Date String and Format

In some situation you may have a string of date. Suppose the string is 01/12/1995 and you want show it differently like Friday, 01 December 1995. In that case, you can use the parse method of SimpleDateFormat class. Just you will have to tell the pattern in which your input date string is, then you can convert it into any pattern you like.

Calendar calendar = Calendar.getInstance();
try {
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    calendar.setTime(sdf.parse("01/12/1995"));
}catch (ParseException e){}

SimpleDateFormat formatter = new SimpleDateFormat("EEEE, dd MMMM yyyy");
String tomorrow = formatter.format(calendar.getTime());

The parsing stage is kept inside a Try Catch Block to avoid parsing error, cause if the pattern you provide is wrong then there will be an error.