Setting Date and Time on Android Using ADB Shell

Setting Date and Time on Android Using ADB Shell

21 May 2024 Stephan Petzl Leave a comment Tech-Help

Managing the date and time on an Android device is crucial for various development and testing scenarios. This guide will walk you through the steps to set the date and time on your Android device using ADB (Android Debug Bridge) shell commands.

Understanding the Command Formats

Android devices use different implementations for command-line tools like date. Depending on the Android version, your device may use either toolbox or toybox binaries. To determine which one your device uses, you can run the following commands:

adb shell toolbox date
adb shell toybox date

The command that returns the current date indicates the available binary. For example, on an Android 6.0+ device:

$ adb shell toybox date
Mon Jul 31 21:09:28 CDT 2017

$ adb shell toolbox date
date: no such tool

Setting Date and Time with Toolbox

If your device uses toolbox, you can set the date and time using the following command format:

adb shell "su 0 toolbox date -s YYYYMMDD.HHmmss"

For instance, to set the date and time to December 31, 2016, at 23:59:59, use:

adb shell "su 0 toolbox date -s 20161231.235959"

Setting Date and Time with Toybox

If your device uses toybox, the command format is slightly different:

adb shell "su 0 toybox date MMDDhhmm[[CC]YY][.ss]"

For example, to set the date and time to December 31, 2016, at 23:59:59, use:

adb shell "su 0 toybox date 123123592016.59"

Handling Time Change Broadcast

After setting the date and time, it might not be immediately visible on the device because it doesn’t trigger a time change broadcast. To ensure the changes are applied instantly, append the following broadcast command:

adb shell 'date MMDDhhmm[[CC]YY][.ss] ; am broadcast -a android.intent.action.TIME_SET'

For example:

adb shell 'date 060910002016.00 ; am broadcast -a android.intent.action.TIME_SET'

Practical Example

Let’s say you need to set the date to October 29, 2020, at 02:01:55 AM. Here’s how you can achieve this:

adb root
adb shell date 102902012020.55

This command follows the format MMDDhhmm[[CC]YY][.ss], where:

  • Month (MM): 10
  • Day (DD): 29
  • Hour (hh): 02
  • Minute (mm): 01
  • Year ([[CC]YY]): 2020
  • Second (.ss): 55

Conclusion

Setting the date and time on an Android device using ADB shell commands can be straightforward if you follow the correct format for your device’s binary. This guide provides a comprehensive approach to ensure you can manage date and time settings effectively.

Enhancing Your Testing Workflow with Repeato

For those looking to streamline their mobile app testing, consider utilizing Repeato. Repeato is a no-code test automation tool for iOS and Android, leveraging computer vision and AI to create, run, and maintain automated tests efficiently. With built-in ADB support, Repeato allows you to execute ADB commands via script steps, ensuring precise timing and sequence for your commands—ideal for tasks like setting date and time during automated tests.

Explore more about how Repeato can enhance your testing workflow on our blog and documentation pages.

Like this article? there’s more where that came from!