calendar.setTimeInMillis(System.currentTimeMillis());是什么意思
时间: 2024-03-19 10:37:14 浏览: 29
`calendar.setTimeInMillis(System.currentTimeMillis())`是将`Calendar`对象的时间设置为当前系统时间的方法。`System.currentTimeMillis()`返回的是从1970年1月1日00:00:00 GMT到当前时间的毫秒数。通过调用`setTimeInMillis()`方法,可以将`Calendar`对象的时间设置为指定的毫秒数所表示的时间。
这个方法的作用是将`Calendar`对象的时间与系统当前时间同步,以便后续对时间的操作和计算。在你提供的引用中,通过调用`now.setTimeInMillis(System.currentTimeMillis())`,将`now`对象的时间设置为当前系统时间。
这样做的目的可能是为了确保`Calendar`对象的时间与系统时间一致,以便后续的时间操作和计算能够准确地进行。
相关问题
c.setTimeInMillis(elapsedTime);
这行代码是Java中用于设置一个Calendar对象的时间的方法。其中elapsedTime是一个long类型的时间戳,表示从1970年1月1日0时0分0秒到现在的毫秒数,通过调用该方法可以将Calendar对象的时间设置为elapsedTime所表示的时间。例如:
```
Calendar c = Calendar.getInstance();
long elapsedTime = System.currentTimeMillis(); // 获取当前时间的时间戳
c.setTimeInMillis(elapsedTime); // 将Calendar对象的时间设置为当前时间
```
这样就可以通过Calendar对象来获取当前时间的年、月、日、时、分、秒等信息。
Android Settings.System设置系统时间
要设置Android系统的时间,可以通过以下代码:
```java
long timeMillis = System.currentTimeMillis();
Settings.System.putInt(getContentResolver(), Settings.System.AUTO_TIME,0);
Settings.System.putIntContentResolver(), Settings.System.AUTO_TIME_ZONE, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Settings.Global.putInt(getContentResolver(), Settings.Global.AUTO_TIME, 0);
Settings.Global.putInt(getContentResolver(), Settings.Global.AUTO_TIME_ZONE, 0);
}
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeMillis);
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
long when = calendar.getTimeInMillis();
if (when / 1000 < Integer.MAX_VALUE) {
SystemClock.setCurrentTimeMillis(when);
}
```
其中,`year`是要设置的年份,`month`是要设置的月份(从0开始,0代表1月),`day`是要设置的日份,`hour`是要设置的小时数,`minute`是要设置的分钟数。
需要注意的是,这个操作需要`android.permission.SET_TIME`权限。另外,`AUTO_TIME`和`AUTO_TIME_ZONE`设置为0表示关闭自动更新时间和时区的功能。为了避免安全问题,这两个设置在Android 6.0及以上版本需要在系统设置中手动开启。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)