How Java timestamps are obtained

Source: Internet
Author: User

Tag: Time () end technical cal span GIF new IMEI mat

Gets the current time stamp
Method one System.currenttimemillis ();//Method two Calendar.getinstance (). Gettimeinmillis ();//Method three new Date (). GetTime ();
Get current time
SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");//Set date format string day = Df.format (new date ());//new Date () to get the current system time, or to use the current timestamp
Get time stamp three ways to perform efficiency comparisons:
Import Java.util.calendar;import java.util.Date;    public class Timetest {private static long _ten_thousand=10000;        public static void Main (string[] args) {long times=1000*_ten_thousand;        Long T1=system.currenttimemillis ();        Testsystem (times);        Long T2=system.currenttimemillis ();         System.out.println (T2-T1);        Testcalander (times);        Long T3=system.currenttimemillis ();         System.out.println (T3-T2);        TestDate (times);        Long T4=system.currenttimemillis ();    System.out.println (T4-T3); } public static void Testsystem (long times) {//use 188 for (int i=0;i<times;i++) {long currenttime=        System.currenttimemillis (); }} public static void Testcalander (long times) {//use 6299 for (int i=0;i<times;i++) {long Curr        Enttime=calendar.getinstance (). Gettimeinmillis (); }} public static void TestDate (long times) {for (int i=0;i<times;i++) {long CurrEnttime=new Date (). GetTime (); }    } }
Execution Result:
133
2372
137

Calendar.getinstance (). Gettimeinmillis () This is the slowest, because it takes more time for the Canlendar to handle time zone problems.

How Java timestamps are obtained

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.