Set date/time using ADB shell

后端 未结 11 2355
野趣味
野趣味 2020-12-04 15:59

I´m trying to set the date/time using the ADB shell but the shell only returns the current time.

I´ve tried:

adb shell date -s YYYYMMDD.HHmmss


        
相关标签:
11条回答
  • 2020-12-04 16:25

    Using the other answers for the current date I get the following error:

    date: bad date '041009502020.23'; Fri Apr 10 09:50:23 CET 2020 != Fri Apr 10 10:50:23 CEST 2020
    

    Using the format @%s (%s being the UNIX timestamp) that the command accepts aswell:

    date: bad date '041009532020.32'; Sun Apr 10 09:53:32 CET 2020 != Fri Apr 10 10:53:32 CEST 2020
    

    Turns out this has something to do with timezones, specifically it seems to have to do with DST summertime (since when I replace 04 with 03 (the month) it works just fine).

    The solution to this problem is to specify the -u option like so:

    # toybox date -u 041009502020.23                                     
    Fri Apr 10 09:50:23 UTC 2020
    

    Depending on your timezone this will set the time to a slightly off value, but after you ran that command you can check with the date command to see how many hours you are off and simply account for that in the set date command!

    0 讨论(0)
  • 2020-12-04 16:31

    To save storage space Android like many other embedded systems uses multi-call binaries to implement its basic command line tools like date.

    Android device may include either toolbox or toybox (or both) binary depending on the version. You can check which implementation of the date tool available on your device by running toolbox date and toybox date commands. Then you can use the one which prints out the current date. For example for an Android 6.0+ device it might look like:

    $ adb shell toybox date
    Mon Jul 31 21:09:28 CDT 2017
    
    $ adb shell toolbox date
    date: no such tool
    

    To set date and time using toolbox date use YYYYMMDD.HHmmss format:

    adb shell "su 0 toolbox date -s 20161231.235959"
    

    In case of toybox date use MMDDhhmm[[CC]YY][.ss] format:

    adb shell "su 0 toybox date 123123592016.59"
    
    0 讨论(0)
  • 2020-12-04 16:31

    For Android 6+, To change DateTime on Emulator:

    Step 1

    adb root
    

    Step 2

    adb shell date MMDDhhmm[[CC]YY][.ss]
    

    For Example to set date to Oct[10] 29 02:01:55 2020 - simply 10/29/2020 02:01:55AM

    adb shell date 102902012020.55
    

    If you don't understand date format, just look at this:

    MM = 10
    DD = 29
    hh = 02
    mm = 01
    [[CC]YY] = 2020 (or can be 20)
    [.ss] = 55
    

    *Year and Second are optional.

    0 讨论(0)
  • 2020-12-04 16:33

    You have to put date value if you want to change it. "-s" changes SET format. Default SET format is "MMDDhhmm[[CC]YY][.ss]".

    I successfully tested the following commands:

    adb root
    adb shell "date `date +%m%d%H%M%Y.%S`"
    
    0 讨论(0)
  • 2020-12-04 16:35
    adb shell date -s `date +%G%m%d.%H%M%S`
    

    At first it gets the current date of your machine and set it on the android target. This approach should work on Linux/Mac/Cygwin.

    0 讨论(0)
提交回复
热议问题