Android Code Snippet Set Date and Time of Device (root needed)

Discussion in 'Code Snippets' started by JohnCody, May 9, 2015.

  1. JohnCody

    JohnCody Active Member Licensed User

    Sub SetDateTime(ToDate As String, ToTime As String)
    'date needs to be in yyyymmdd format
        'time needs to be in hhmmss 24-hour
        'date -s 20120419.024012; \n
    Dim Command, Runner As String
    Dim StdOut, StdErr As StringBuilder
    Dim Result As Int
    Dim Ph As Phone

        Runner = 
    File.Combine(File.DirInternalCache, "runner")
        Command = 
    File.Combine(File.DirInternalCache, "command")
    File.WriteString(File.DirInternalCache, "runner""su < " & Command)
    File.WriteString(File.DirInternalCache, "command""date -s " & ToDate & "." & ToTime & "; \n" & CRLF & "exit")
        Result = Ph.Shell(
    "sh"Array As String(Runner), StdOut, StdErr)
    End Sub
    lemonisdead likes this.
  2. NJDude

    NJDude Expert Licensed User

    That code doesn't really work, some devices do not have "su".
  3. JohnCody

    JohnCody Active Member Licensed User

    If you have a rooted device, then you probably already have super user installed or can easily install it.
  4. wes58

    wes58 Active Member Licensed User

    Thanks. It works fine, but one comment about date format. It should be in "yyyyMMdd" format (i.e. MM for month not mm!)