Android
Current Time
Current Date
Date and Time API
Java datetime

How to get current time and date in Android

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

In Android development, handling date and time is crucial for various functionalities like setting reminders, logging activities, or syncing data with a server. Fortunately, Android provides robust libraries and APIs to effectively work with date and time. This article explores different approaches to getting the current time and date in Android, supplemented by technical explanations and practical examples.

Using java.util.Date and java.text.DateFormat

java.util.Date is a commonly used class to capture current date and time. However, it's worth mentioning that Date essentially serves as a container for date-time and lacks extensive functionality. To format the date, java.text.DateFormat can be used.

Example:

java
1import java.util.Date;
2import java.text.DateFormat;
3import java.text.SimpleDateFormat;
4
5public String getCurrentDateAndTime() {
6    Date currentDate = new Date();
7    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
8    return dateFormat.format(currentDate);
9}

Explanation:

  • Date currentDate = new Date();: This line creates a new Date object containing the current date and time.
  • DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");: Initializes a DateFormat object to specify the output format.
  • dateFormat.format(currentDate);: Formats the Date object according to the specified pattern.

Using java.util.Calendar

Calendar is another class available in Java to work with date and time. It's more flexible than Date for handling specific date and time fields.

Example:

java
1import java.util.Calendar;
2
3public String getCurrentDateAndTimeUsingCalendar() {
4    Calendar calendar = Calendar.getInstance();
5    int year = calendar.get(Calendar.YEAR);
6    int month = calendar.get(Calendar.MONTH) + 1; // Months are 0-based in Calendar
7    int day = calendar.get(Calendar.DAY_OF_MONTH);
8    int hour = calendar.get(Calendar.HOUR_OF_DAY);
9    int minute = calendar.get(Calendar.MINUTE);
10    int second = calendar.get(Calendar.SECOND);
11    
12    return day + "/" + month + "/" + year + " " + hour + ":" + minute + ":" + second;
13}

Explanation:

  • Calendar calendar = Calendar.getInstance();: Obtains a Calendar object set to the current date and time.
  • Each field such as YEAR, MONTH, DAY_OF_MONTH, etc., can be accessed individually.

Using java.time (For Android API Levels >= 26)

The java.time package introduced in Java 8 offers a modern approach to handling date and time, and it became natively available in Android from API level 26 (Android 8.0).

Example:

java
1import java.time.LocalDateTime;
2import java.time.format.DateTimeFormatter;
3
4public String getCurrentDateAndTimeUsingJavaTime() {
5    LocalDateTime currentDateTime = LocalDateTime.now();
6    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
7    return currentDateTime.format(formatter);
8}

Explanation:

  • LocalDateTime currentDateTime = LocalDateTime.now();: Retrieves the current date and time.
  • DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");: Defines a format for the output.
  • currentDateTime.format(formatter);: Formats the LocalDateTime according to the specified pattern.

Using android.text.format.DateFormat

For Android-specific date formatting, android.text.format.DateFormat provides utility methods.

Example:

java
1import android.text.format.DateFormat;
2import java.util.Date;
3
4public CharSequence getCurrentDateAndTimeUsingAndroidDateFormat() {
5    Date currentDate = new Date();
6    return DateFormat.format("dd/MM/yyyy HH:mm:ss", currentDate);
7}

Explanation:

  • DateFormat.format("dd/MM/yyyy HH:mm:ss", currentDate);: Utilizes Android's DateFormat to format the date.

Summary

Here’s a summary table comparing the different methods discussed:

MethodAPI Level RequirementAdvantagesDisadvantages
java.util.Date and DateFormatAPI Level 1Simple and widely usedLimited flexibility, deprecated methods
java.util.CalendarAPI Level 1Greater flexibilityVerbose
java.timeAPI Level 26Modern API, thread-safeRequires higher API level
android.text.format.DateFormatAPI Level 1Android-specific, simple formattingLimited to Android

Conclusion

Understanding how to get and format current date and time is essential for Android developers across various application scenarios. Depending on the API level and specific needs, you can choose among several methods discussed. This flexibility ensures you can implement date and time functionality efficiently in your Android applications.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.