Android set system time and date method

Source: Internet
Author: User
Tags time and date

First, set the system time
  
 
  1. static void setTime(Context context, int hourOfDay, int minute) {
  2. Calendar c = Calendar.getInstance();
  3. c.set(Calendar.HOUR_OF_DAY, hourOfDay);
  4. c.set(Calendar.MINUTE, minute);
  5. c.set(Calendar.SECOND, 0);
  6. c.set(Calendar.MILLISECOND, 0);
  7. long when = c.getTimeInMillis();
  8. /span>if ( when / 1000 < integer max_value {
  9. ((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)).setTime(when);
  10. }
  11. }
Second, set the system date
  
 
  1. static void SetDate ( context Context int year int month int day ) {
  2. Calendar c = Calendar.getInstance();
  3. c.set(Calendar.YEAR, year);
  4. c.set(Calendar.MONTH, month);
  5. c.set(Calendar.DAY_OF_MONTH, day);
  6. long when = c.getTimeInMillis();
  7. /span>if ( when / 1000 < integer max_value {
  8. ((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)).setTime(when);
  9. }
  10. }
You also need to add the relevant permissions
 
   
  
  1. <uses-permission android:name="android.permission.SET_TIME" />



From for notes (Wiz)

Android set system time and date method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.