A variety of ways Android acquires system time _android

Source: Internet
Author: User

There are several ways to get system time in Android, which can be divided into Java Calendar class acquisition, Java.util.date class implementation, and Android.

The summary is as follows:

Method One:

void GetTime1 () { 
    long time=system.currenttimemillis ();//long now = Android.os.SystemClock.uptimeMillis (); 
    SimpleDateFormat format=new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); 
    Date D1=new date (time); 
    String T1=format.format (d1); 
    LOG.E ("msg", T1); 
  } 

Method Two:

SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd-hh:mm:ss"); 
    String T=format.format (New Date ()); 
    LOG.E ("msg", t); 

Method Three:

void GetTime3 () { 
  Calendar calendar = calendar.getinstance (); 
  String created = Calendar.get (calendar.year) + "Year" 
      + (Calendar.get (calendar.month) +1) + "Month"//from 0 calculation 
      + Calendar.get (Calendar.day_of_month) + "Day" 
      + calendar.get (calendar.hour_of_day) + "time" 
      + calendar.get ( Calendar.minute) + "min" +calendar.get (Calendar.second) + "s"; 
  LOG.E ("MSG", created); 
  } 

Method Four:

void GetTime4 () {Time 
    t=new time (),//or time T=new time ("Gmt+8"), plus time zone data. 
    T.settonow ();//Get the system time. 
    String time=t.year+ "year" + (t.month+1) + "month" +t.monthday+ "Day" +t.hour+ "H" +t.minute+ "M" +t.second; 
    LOG.E ("MSG", time); 
  } 

Get Sunday Period:

Calendar calendar = Calendar.getinstance (); 
      int day = Calendar.get (Calendar.day_of_week); 
      String today = null; 
      if (day = = 2) {Today 
        = ' Monday '; 
      } else if (day = = 3) {Today 
        = ' Tuesday '; 
      } else if (day = = 4) { 
        tod ay = "Wednesday"; 
      }  else if (day = = 5) {Today 
        = ' Thursday '; 
      } else if (day = = 6) {Today 
        = ' Friday '; 
      } else if (day = 7) {Today 
        = "Saturday"; 
      } else if (day = = 1) {Today 
        = "Sunday"; 
      } 
      System.out.println ("Today's is:-" + today); 

Finally, the date format, date format is usually implemented using the SimpleDateFormat class, where the date format can not be defined at their own discretion, mainly in the following several forms:

SimpleDateFormat f1= new SimpleDateFormat (); There are no formatting parameters, we use the default date format.
System.out.println (New Date ()) (f.formate);

The date format for the code output is:12-3-22 pm 4:36

SimpleDateFormat f4= New SimpleDateFormat ("Today is" + "yyyy mm month DD Day E KK-point mm);
" Different date formats can be displayed according to different style requests, and the E-parameter
System.out.println (F4.format (New Date ()) can be added to the week;
The date format for the code output is: Today is March 22, 2012 Thursday 16:46
SimpleDateFormat formater = new SimpleDateFormat ("YyyyMMdd hh:mm:ss");
System.out.println ("date to String" +formater.format (new Date ());
Similar common forms also include YyMMdd hh:mm:ss yyyy-mm-dd hh:mm:ss dd-mm-yyyy hh:mm:ss


It is usually necessary to convert a specific date to a millisecond or timestamp form, as follows:

Text-> Timestamp, Date-> Timestamp

 Timestamp T;
 SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");
 Try ... {
  T = new Timestamp (Format.parse ("2007-07-19 00:00:00"). GetTime ());
 catch (ParseException e) ... {
  e.printstacktrace ();
 }
 Timestamp T;
 SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");
 t = new Timestamp (new Date (). GetTime ());

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.