Acquisition of system date and time in Android

Source: Internet
Author: User
Tags dateformat

[Java]View Plaincopyprint?
  1. Import Java.text.SimpleDateFormat;
  2. SimpleDateFormat formatter = new SimpleDateFormat ("yyyy mm month DD Day HH:mm:ss");
  3. Date curdate = new Date (System.currenttimemillis ()); Get current time
  4. String str = Formatter.format (curdate);
 

You can get the current year or month, or you can 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 ());
 

If you want to get the current month and year, you can write this (just get the same time or seconds):

Java Code

[Java]View Plaincopyprint?
    1. SimpleDateFormat sdf=New SimpleDateFormat ("yyyy-mm");
    2. String Date=sdf.format (new Java.util.Date ());

Of course, there are times when you can specify a time zone (wait):

[Java]View Plaincopyprint?
    1. Df=dateformat.getdatetimeinstance (Dateformat.full,dateformat.full,locale.china);
    2. System.out.println (Df.format (new Date ()));

how to get the Android system time is 24 hour or 12 hour system

[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 (")" )
  5. {
  6. LOG.I ("activity","24");
  7. }

[Java]View Plaincopyprint?
    1. Calendar C = calendar.getinstance ();
    2. Get system date: Year = C.get (calendar.year)
    3. month = C.GRT (calendar.month)
    4. Day = C.get (calendar.day_of_month)
    5. Acquisition system time: Hour = C.get (Calendar.hour_of_day);
    6. minute = C.get (Calendar.minute)

Use Calendar to get

[Java]View Plaincopyprint?
  1. Calendar C = calendar.getinstance ();
  2. Get system date: Year = C.get (calendar.year)
  3. month = C.GRT (calendar.month)
  4. Day = C.get (calendar.day_of_month)
  5. Acquisition system time: Hour = C.get (Calendar.hour_of_day);
  6. minute = C.get (Calendar.minute)
  7. Calendar C = calendar.getinstance ();
  8. Get system date: Year = C.get (calendar.year)
  9. month = C.GRT (calendar.month)
  10. Day = C.get (calendar.day_of_month)
  11. Acquisition system time: Hour = C.get (Calendar.hour_of_day);
  12. minute = C.get (Calendar.minute)

Use Time to get

[Java]View Plaincopyprint?
    1. Time t=new Time ();  //or Time T=new time ("Gmt+8"), plus time zone information.
    2. T.settonow ();  //Get 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;



The only disadvantage is that the removal time is only 24 hours mode.

Acquisition of system date and time in Android

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.