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 TimeStep 1: Create an empty activity project
Step 2: Working with the activity_main.xml file
Understanding how to format date and time in Android using SimpleDateFormat:
Character to be used | Output |
---|---|
dd | Date in numeric value |
E | Day in String (short form, e.g., Mon) |
EEEE | Day in String (full form, e.g., Monday) |
MM | Month in numeric value |
yyyy | Year in numeric value |
LLL | Month in String (short form, e.g., Mar) |
LLLL | Month in String (full form, e.g., March) |
HH | Hour in numeric value (24-hour format) |
KK | Hour in numeric value (12-hour format) |
mm | Minute in numeric value |
ss | Seconds in numeric value |
aaa | Displays AM or PM (according to 12-hour format) |
z | Displays the time zone of the region |
SimpleDateFormat
and Calendar
to achieve different formatting options.TextView
to display formatted date and time values on the Android app interface.SimpleDateFormat
class to define date and time formats in Android.Calendar
class to retrieve and manipulate date and time values.TextView
.dd.MM.yyyy HH:mm:ss aaa z
(e.g., 12.04.2024 15:30:00 PM GMT)dd-MM-yyyy HH:mm:ss aaa z
(e.g., 12-04-2024 15:30:00 PM GMT)dd/MM/yyyy HH:mm:ss aaa z
(e.g., 12/04/2024 15:30:00 PM GMT)dd.LLL.yyyy HH:mm:ss aaa z
(e.g., 12.Apr.2024 15:30:00 PM GMT)dd.LLLL.yyyy HH:mm:ss aaa z
(e.g., 12.April.2024 15:30:00 PM GMT)E.LLLL.yyyy HH:mm:ss aaa z
(e.g., Mon.April.2024 15:30:00 PM GMT)EEEE.LLLL.yyyy KK:mm:ss aaa z
(e.g., Monday.April.2024 03:30:00 PM GMT)