VOOZH about

URL: https://www.geeksforgeeks.org/android/date-and-time-formatting-in-android/

⇱ Date and Time Formatting in Android - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Date and Time Formatting in Android

Last Updated : 23 Jul, 2025

Date and Time in Android are formatted using the SimpleDateFormat library from Java, using Calendar instance which helps to get the current system date and time. The current date and time are of the type Long which can be converted to a human-readable date and time. In this article, it's been discussed how the Date and Time values can be formatted in various formats and displayed. Have a look at the following image to get an idea of the entire discussion.

👁 Date and Time Formatting in Android

Steps to Format the Date and Time in Android

Step 1: Create an empty activity project

Step 2: Working with the activity_main.xml file

  • The main layout of the activity file containing 8 TextViews. One to show the current system date and time value in Long type, and others to display the same date and time value in a formatted human-readable way.
  • To implement the UI invoke the following code inside the activity_main.xml file.

Step 3: Working with theMainActivity file

Understanding the way of formatting the date and time in Android using SimpleDateFormat 

  • Firstly, the instance of the Calendar is created and the desired format of the date and time to be shown is passed to the SimpleDateFormat method. The String should include the following characters and one may include the separators like -, / etc.
  • The below table includes the characters to be used to generate the most used common pattern of date and time.

Character to be used

Output

ddDate in numeric value
EDay in String (short form. Ex: Mon)
EEEEDay in String (full form. Ex: Monday)
MMMonth in numeric value
yyyyYear in numeric value
LLLMonth in String (short form. Ex: Mar)
LLLLMonth in String (full form. Ex: March)
HHHour in numeric value (24hrs timing format)
KKHour in numeric value (12hrs timing format)
mmMinute in numeric value
ssSeconds in numeric value
aaaDisplays AM or PM (according to 12hrs timing format)
zDisplays the time zone of the region
  • Refer to the following code and its output to get a better idea of the above table.

Output:

👁 Date and Time Formatting in Android Output
Comment

Explore