Acquisition of system date and time in Android

Source: Internet
Author: User

[Java]
View plaincopyprint?
  1. Import java. Text. simpledateformat;
  2. Simpledateformat formatter = new simpledateformat ("mm DD, yyyy hh: mm: SS ");
  3. Date curdate = new date (system. currenttimemillis (); // obtain the current time
  4. String STR = formatter. Format (curdate );

Import Java. text. simpledateformat; </P> <p> simpledateformat formatter = new simpledateformat ("YYYY mm dd hh: mm: SS "); <br/> date curdate = new date (system. currenttimemillis (); // obtain the current time <br/> string STR = formatter. format (curdate); <br/> 

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


[Java]
View plaincopyprint?
  1. Simpledateformat sdateformat = new simpledateformat ("yyyy-mm-dd hh: mm: SS ");
  2. String date = sdateformat. Format (New java. util. Date ());

Simpledateformat sdateformat = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); <br/> string date = sdateformat. format (New Java. 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 ):

    Java code

    [Java]
    View plaincopyprint?
    1. Simpledateformat SDF = new simpledateformat ("yyyy-mm ");
    2. String date = SDF. Format (New java. util. Date ());

    Simpledateformat SDF = new simpledateformat ("yyyy-mm"); <br/> string date = SDF. Format (New java. util. Date (); <br/>

    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 (new date ()));

    DF = dateformat. getdatetimeinstance (dateformat. Full, dateformat. Full, locale. China); <br/> system. Out. println (DF. Format (new date ())); 

     

    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. }

    Contentresolver CV = This. getcontentresolver (); <br/> string strtimeformat = android. provider. settings. system. getstring (CV, <br/> android. provider. settings. system. time_12_24); </P> <p> If (strtimeformat. equals ("24") <br/>{< br/> log. I ("activity", "24"); <br/>} 

     

    [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)

    Calendar c = calendar. getinstance (); <br/> system date: year = C. get (calendar. year) <br/> month = C. GRT (calendar. month) <br/> day = C. get (calendar. day_of_month) <br/> obtain the system time: hour = C. get (calendar. hour_of_day); <br/> 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)

    Calendar c = calendar. getinstance (); <br/> system date: year = C. get (calendar. year) <br/> month = C. GRT (calendar. month) <br/> day = C. get (calendar. day_of_month) <br/> obtain the system time: hour = C. get (calendar. hour_of_day); <br/> minute = C. get (calendar. minute) <br/> calendar c = calendar. getinstance (); <br/> system date: year = C. get (calendar. year) <br/> month = C. GRT (calendar. month) <br/> day = C. get (calendar. day_of_month) <br/> obtain the system time: hour = C. get (calendar. hour_of_day); <br/> minute = C. get (calendar. minute)

     

     

    Obtain with time

    [Java]
    View plaincopyprint?
    1. Time t = new time (); // or time t = new time ("GMT + 8"); plus the time zone information.
    2. T. settonow (); // obtain the system time.
    3. Int year = T. Year;
    4. Int month = T. month;
    5. Int date = T. monthday;
    6. Int hour = T. hour; // 0-23
    7. Int minute = T. minute;
    8. Int second = T. Second;

    Time t = new time (); // or time t = new time ("GMT + 8"); plus the time zone information. <Br/> T. settonow (); // obtain the system time. <Br/> int year = T. year; <br/> int month = T. month; <br/> int date = T. monthday; <br/> int hour = T. hour; // 0-23 <br/> int minute = T. minute; <br/> int second = T. second;

    The only disadvantage is that the retrieval time is only in 24-hour mode.

    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.