的Andr​​oid如何通过应用程序设置时区应用程序、时区、Andr、oid

2023-09-07 13:19:51 作者:等你到时光的尽头

Android提供了一个名为SET_TIME_ZONE与操作系统权限级别危险的许可。有谁知道这个权限给定的应用程序,如何应用程序设置时区?

Android provides a permission called "SET_TIME_ZONE" with OS permission level "dangerous". Does anyone know that given an application with this permission, how can the app set the time zone ?

感谢。

推荐答案

有关设置时区Programetically你需要使用Date类。 看到它的参考文档这里。

For Setting of the Time Zone Programetically you need to use the Date Class. See its Reference Documents here.

您需要使用的SimpleDateFormat 类的 setTimeZone()方法。

下面是示例code根据美国设置时区

Following is sample code for settings Time Zone of according to America

// First Create Object of Calendar Class
Calendar calendar = Calendar.getInstance();        
// Now Set the Date using DateFormat Class
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss z"); 
// Finally Set the time zone using SimpleDateFormat Class's setTimeZone() Method
sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));