Java Gets the current timestamp (reprinted) using new Date () and System.currenttimemillis ()

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/wuchen/archive/2012/06/30/2570746.html

In the development process, often many people are accustomed to use the new date () to get the current time, it is more convenient to use, but also to get the current time related to all aspects of information, such as get hours, minutes and so on, but also can format the output, contains the information is relatively rich. But sometimes you don't need to get that much information, you just have to care about the number of milliseconds it returns, such as getTime(). In order to get this timestamp, many people also like to use the new date (). GetTime () to get, I see no problem, but in fact there is no need.

In fact, look at the Java source code will know:

   Public Date ()  {    this(System.currenttimemillis ());  }

It's already obvious that what new Date () is doing is actually calling System.currenttimemillis (). If it is just a need or a millisecond, then you can use System.currenttimemillis () instead of the new Date (), which is a bit more efficient. And many people like to use the new Date () multiple times in the same method, usually the performance is 1.1 points to consume, here can actually declare a reference.

Java Gets the current timestamp (reprinted) using new Date () and System.currenttimemillis ()

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.