Acquisition of system date and time in Android

Source: Internet
Author: User
    1. ImportJava. Text. simpledateformat;
    2. Simpledateformat formatter =NewSimpledateformat ("Yyyy-mm-dd hh: mm: SS");
    3. Date curdate =NewDate (system. currenttimemillis ());// Obtain the current time
    4. String STR = formatter. Format (curdate );

    You can obtain the current date and month, or write separately:

    [Java] View plaincopyprint?

      1. Simpledateformat sdateformat =NewSimpledateformat ("Yyyy-mm-dd hh: mm: SS");
      2. String date = sdateformat. Format (NewJava. util. Date ());

     

    If you want to obtain the current year and month, you can write it like this (only get the same time or second ):

    JavaCode [Java] View plaincopyprint?

      1. Simpledateformat SDF =NewSimpledateformat ("Yyyy-mm");
      2. String date = SDF. Format (NewJava. util. Date ());

     

    Of course, you can also specify the time zone (to be ):

    [Java] View plaincopyprint?

      1. DF = dateformat. getdatetimeinstance (dateformat. Full, dateformat. Full, locale. China );
      2. System. Out. println (DF. Format (NewDate ()));

     

     

     

     

    How to obtain whether the Android system is in the 24-hour or 12-hour format

    [Java] View plaincopyprint?

    1. Contentresolver CV =This. Getcontentresolver ();
    2. String strtimeformat = Android. provider. settings. system. getstring (CV,
    3. Android. provider. settings. system. time_12_24 );
    4. If(Strtimeformat. Equals ("24"))
    5. {
    6. Log. I ("Activity","24");
    7. }

     

     

    [Java] View plaincopyprint?

      1. Calendar c = calendar. getinstance ();
      2. Obtain the system date: year = C. Get (calendar. Year)
      3. Month = C. GRT (calendar. month)
      4. Day = C. Get (calendar. day_of_month)
      5. Obtain the system time: hour = C. Get (calendar. hour_of_day );
      6. Minute = C. Get (calendar. Minute)

     

     

     

    Obtain from calendar

    [Java]View plaincopyprint?

    1. Calendar c = calendar. getinstance ();
    2. Obtain the system date: year = C. Get (calendar. Year)
    3. Month = C. GRT (calendar. month)
    4. Day = C. Get (calendar. day_of_month)
    5. Obtain the system time: hour = C. Get (calendar. hour_of_day );
    6. Minute = C. Get (calendar. Minute)
    7. Calendar c = calendar. getinstance ();
    8. Obtain the system date: year = C. Get (calendar. Year)
    9. Month = C. GRT (calendar. month)
    10. Day = C. Get (calendar. day_of_month)
    11. Obtain the system time: hour = C. Get (calendar. hour_of_day );
    12. Minute = C. Get (calendar. Minute)

     

     

     

    Obtain with time

     

    [Java] View plaincopyprint?

    1. Time t =NewTime ();// Or time t = new time ("GMT + 8"); Add the time zone information.
    2. T. settonow ();// Obtain the system time.
    3. IntYear = T. Year;
    4. IntMonth = T. month;
    5. IntDate = T. monthday;
    6. IntHour = T. hour;// 0-23
    7. IntMinute = T. minute;
    8. IntSecond = T. Second;

     

Related Article

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.