lightbulb_outline Please take our October 2018 developer survey. Start survey

TimeManager

public class TimeManager
extends Object

java.lang.Object
   ↳ com.google.android.things.device.TimeManager


This class provides access to device settings related to time. This includes the following operations:

  • Time format (12-hour or 24-hour)
  • Time zone
  • Auto sync time from a Network Time Protocol (NTP) server

To control device settings using TimeManager, first request the permission com.google.android.things.permission.SET_TIME in your AndroidManifest.xml, then obtain an instance of the class and set the properties appropriate for your app.

 TimeManager timeManager = TimeManager.getInstance();
 // Use 24-hour time
 timeManager.setTimeFormat(TimeManager.FORMAT_24);

 // Set time zone to Eastern Standard Time
 timeManager.setTimeZone("America/New_York");

 // Set clock time to noon
 Calendar calendar = Calendar.getInstance();
 calendar.set(Calendar.MILLISECOND, 0);
 calendar.set(Calendar.SECOND, 0);
 calendar.set(Calendar.MINUTE, 0);
 calendar.set(Calendar.HOUR_OF_DAY, 12);
 long timeStamp = calendar.getTimeInMillis();
 timeManager.setTime(timeStamp);

To get the current time or time zone, obtain a Calendar instance:

 Calendar calendar = Calendar.getInstance();
 long now = calendar.getTimeInMillis();
 TimeZone current = calendar.getTimeZone();

Current values for other time-related settings can be read from Settings.Global.

Summary

Constants

int FORMAT_12

12 hour time format

int FORMAT_24

24 hour time format

Public methods

static TimeManager getInstance()

Returns the TimeManager instance for this application.

void setAutoTimeEnabled(boolean enabled)

Sets whether or not wall clock time should sync with automatic time updates from NTP.

void setTime(long millis)

Set the system wall clock time.

void setTimeFormat(int timeFormat)

Sets the time format for the device.

void setTimeZone(String timeZone)

Set the system's persistent default time zone.

Inherited methods

From class java.lang.Object

Constants

FORMAT_12

int FORMAT_12

12 hour time format

Constant Value: 12 (0x0000000c)

FORMAT_24

int FORMAT_24

24 hour time format

Constant Value: 24 (0x00000018)

Public methods

getInstance

TimeManager getInstance ()

Returns the TimeManager instance for this application.

Returns
TimeManager The TimeManager instance.

setAutoTimeEnabled

void setAutoTimeEnabled (boolean enabled)

Sets whether or not wall clock time should sync with automatic time updates from NTP.

Parameters
enabled boolean: if true, automatic time updates are enabled.

Throws
IllegalStateException if the underlying system service is not ready
RuntimeException if the underlying system service encountered an error
SecurityException if the calling context does not have com.google.android.things.permission.SET_TIME permission

setTime

void setTime (long millis)

Set the system wall clock time. This method does not guarantee persistence past reboot. This method also disables automatic time updates from NTP.

Parameters
millis long: time in milliseconds since the Unix Epoch

Throws
IllegalStateException if the underlying system service is not ready
RuntimeException if the underlying system service encountered an error
SecurityException if the calling context does not have com.google.android.things.permission.SET_TIME permission

setTimeFormat

void setTimeFormat (int timeFormat)

Sets the time format for the device. This should be either FORMAT_12 or FORMAT_24.

Parameters
timeFormat int: the timeFormat to be set

Throws
IllegalStateException if the underlying system service is not ready
RemoteException if the underlying system service encountered an error
SecurityException if the calling context does not have com.google.android.things.permission.SET_TIME permission

setTimeZone

void setTimeZone (String timeZone)

Set the system's persistent default time zone. This sets the time zone for all apps, even after reboot.

Parameters
timeZone String: one of the Olson ids from the list returned by TimeZone.getAvailableIDs()

Throws
IllegalStateException if the underlying system service is not ready
RuntimeException if the underlying system service encountered an error
SecurityException if the calling context does not have com.google.android.things.permission.SET_TIME permission