Get Date and time

Source: Internet
Author: User
Tags add time

Android files are recommended to replace calendar with time. The load on the CPU with time is small. is especially important when writing widgets.

Time T=new time (); or time T=new time ("gmt+8"); Add time zone information.

[Java]View Plaincopy
  1. Package itokit.com;
  2. Import android.app.Activity;
  3. Import Android.os.Bundle;
  4. Import Android.text.format.Time;
  5. Import Android.widget.TextView;
  6. Public class ShowTime extends Activity {
  7. /** Called when the activity is first created. * /
  8. @Override
  9. public void OnCreate (Bundle savedinstancestate) {
  10. super.oncreate (savedinstancestate);
  11. Setcontentview (R.layout.main);
  12. TextView Mytextview = (TextView) Findviewbyid (R.id.mytextview);
  13. Time time = new Time ("gmt+8");
  14. Time.settonow ();
  15. int year = Time.year;
  16. int month = Time.month;
  17. int day = Time.monthday;
  18. int minute = Time.minute;
  19. int hour = Time.hour;
  20. int sec = Time.second;
  21. Mytextview.settext ("Current time:" + year +
  22. "Year" + month +
  23. "Month" + Day +
  24. "Day" + Hour +
  25. "when" + Minute +
  26. "min" + sec +
  27. "seconds");
  28. }
  29. }


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

Long Time=system.currenttimemillis ();
Final Calendar mcalendar=calendar.getinstance ();
Mcalendar.settimeinmillis (time);
Hours of acquisition: Mhour=mcalendar.get (Calendar.hour);
Get minutes: Mminuts=mcalendar.get (Calendar.minute);

How to get the Android system time is 24 hour or 12 hour system
Contentresolver CV = this.getcontentresolver ();
String Strtimeformat = android.provider.Settings.System.getString (CV,
ANDROID.PROVIDER.SETTINGS.SYSTEM.TIME_12_24);

if (Strtimeformat.equals ("24"))

{
LOG.I ("Activity", "24");
}

Calendar C = calendar.getinstance ();
Get system date: Year = C.get (calendar.year)
month = C.GRT (calendar.month)
Day = C.get (calendar.day_of_month)
Acquisition system time: Hour = C.get (Calendar.hour_of_day);
minute = C.get (Calendar.minute)


use calendar to get
Calendar C = calendar.getinstance ();

Get system date: Year = C.get (calendar.year)
month = C.GRT (calendar.month)
Day = C.get (calendar.day_of_month)
Acquisition system time: Hour = C.get (Calendar.hour_of_day);
minute = C.get (Calendar.minute)
Calendar C = calendar.getinstance ();
Get system date: Year = C.get (calendar.year)
month = C.GRT (calendar.month)
Day = C.get (calendar.day_of_month)
Acquisition system time: Hour = C.get (Calendar.hour_of_day);
minute = C.get (Calendar.minute)

Get Date and time

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.