How does Android get the current time?

Source: Internet
Author: User

 

Java code
  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; simpledateformat formatter = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); Date curdate = new date (system. currenttimemillis (); // obtain the current time string STR = formatter. format (curdate );

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

Java code
  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");    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
  1. Simpledateformat SDF = new simpledateformat ("yyyy-mm ");
  2. String date = SDF. Format (New java. util. Date ());
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM"); String date=sdf.format(new java.util.Date());

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

DF = dateformat. getdatetimeinstance (dateformat. Full, dateformat. Full, locale. China );
System. Out. println (DF. Format (new date ()));

 

 

========================================================== ======================================

Add QQ: 284651035 (this blogger) for help)

Weibo: http://weibo.com/cnbailu (Bai Lu)

Http://weibo.com/u/2717396075 (featured jokes)

Mr Chen Weibo: http://weibo.com/redarmychen Java direction

Miss Li Weibo: http://weibo.com/csdnliwei PHP direction

Mobile (mobile) Software Development Exchange Group: 57636364(Please note that the application is on the csdn blog. Remember to change your business card after entering the group.) Thank you!

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.