Skip to content

Most visited

Recently visited

navigation

TimeManager

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, obtain an instance of the class and set the properties appropriate for your app.

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

 // 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 constructors

TimeManager()

Public methods

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 constructors

TimeManager

TimeManager ()

Public methods

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.CHANGE_TIME permission

setTime

void setTime (long millis)

Set the system wall clock time. This method does not guarentee persistance 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.CHANGE_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.CHANGE_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.CHANGE_TIME permission
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience. (Dec 2017 Android Platform & Tools Survey)