Java and C # implementation time conversion

Source: Internet
Author: User

A web Service was made for two days to record the operation time, defined as long data, because the current time in Java can be expressed as a long type.

In Java, you can use System.currenttimemillis () to get the long form of the current time, which is labeled as the number of milliseconds from January 1, 1970 to the current millisecond. The Web service is written in Java, which turns this long data into timestamp and then into MySQL, so you can pass this value directly to the Web service that calls us.

But. NET to calculate the time is not the same way, it is the unit of calculation is ticks, on the TICKS,MSDN is said:

A single tick represents one hundred nanoseconds or one ten-millionth of a second. The value of this property represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, Ja Nuary 1, 0001.

Is from the year of 1st, midnight to a specified time of one out of 10,000 seconds, in order to compare with Java, say one out of 10,000 milliseconds.

(System.datetime.utcnow.ticks-new DateTime (1970, 1, 1, 0, 0, 0). Ticks)/10000;

If you want to get the same result as System.currenttimemillis () in Java, you can write it as above, or you can:

TimeSpan ts=new TimeSpan (system.datetime.utcnow.ticks-new DateTime (1970, 1, 1, 0, 0, 0). Ticks);

(long) Ts. TotalMilliseconds;

Note that this is the System.DateTime.UtcNow instead of the System.DateTime.Now, because we are in the East eight, and if we use the latter form, we will find that the time is 8 hours worse than imagined.

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.