Gettimeinmillis () vs. datetime. Now. ticks -- difference between Java and. Net in obtaining long integer values in milliseconds

Source: Internet
Author: User

From http://blog.csdn.net/laobai_2006/article/details/3124719

 

In this case, the system is maintained by laobai.com, which uses Java to generate a class with unique identifiers, mainly relying on "calendar. getinstance (). gettimeinmillis () "to obtain the so-called long integer value in milliseconds. When conditions are limited, the old white now needs to use. Net to implement the same function, so it is assumed that "datatime. Now. ticks" is used ".

As you may know, "datatime. now. although ticks is indeed of the long (int64) type, the time unit of the value is different from that of Java. It is 100 nanosecond (100 nanoseconds, 10-7 seconds ), instead of 1 Millisecond (1 millisecond, 10-3 seconds ). This is not a big deal (it's not big yet, it's an order of magnitude). Divide the value of "datetime. Now. ticks" by 10000 and it will be over ...... Not that easy!

After some investigation and evidence collection, Bai found that Java was counted in milliseconds starting from 00:00:00. 000:

"Gettimeinmillis ()... returns: the current time as UTC milliseconds from the epoch." fromhttp: // java.sun.com/j2se/1.5.0/docs/api/java/util/calendar.html#gettimeinmillis ()

The so-called epoch is the moment 00:00:00. 000 (I don't know whether there is any story behind it, please share it with others ).

What about. Net? She calculated from 0001-01-01 00:00:00. 000, and the number is much larger. Datetime. ticks in msdn says this:

"The value of this property represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001." From http://msdn.microsoft.com/en-us/library/system.datetime.ticks (vs.80). aspx

Therefore, apart from the unit in which values are used, the time start point of the two counts is also different. The final conclusion is. net System. datetime. now. ticks to implement Java. util. calendar. getinstance (). if gettimeinmillis (), you can take something similar to "(datetime. now. ticks-New datetime (1970, 1, 1 ). ticks)/10000 "method, of course, I use" (datetime. now. ticks-621355968000000000)/10000 ", meaning one.

Let's see what I have said this time:

 

Java. util. Calendar. gettimeinmillis () System. datetime. ticks
Data Type Long Long
Time Unit Millisecond 100 Nanosecond
Timing start point 00:00:00. 000 0001-01-01 00:00:00. 000

 

Afterwards, some enthusiastic friends reminded me that division for long will produce an error (when division is not allowed), and they will use bigdecimal for Java developers, otherwise they will be laughed. That's it. Hurry up. NET should use decimal?

 

    1. Public long gettimeinmillis ()
    2. {
    3. Return decimal. toint64 (decimal. Divide (datetime. Now. ticks-621355968000000000,100 00 ));
    4. }

Later, I found that this is actually a problem of mutual conversion between Unix time and. NET Time notation. I remember that LINUX started with epoch, but I didn't know that there was a student named "epoch" at 00:00:00. 000 ".

 

It's not over yet. Please read it down !!!

Important: enthusiastic reader mobdx_19840908 keenly pointed out the fatal error in this article (see the comments )~~~ Simply put, you don't pay attention to the time zone when using datetime. Now! The old white himself mentioned above that Java is based on UTC, and it has been verified that. Net corresponds to datetime. utcnow instead of datetime. Now!

 

Therefore, the final conclusion is:

 

Return decimal. toint64 (decimal. Divide (datetime. Now. ticks-New datetime (1970, 1, 1,8, 0, 0). ticks, 10000 ))

 

Or

 

Return decimal. toint64 (decimal. Divide (datetime.Utcnow. Ticks-621355968000000000,100 00 ));

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.