Home  >  Article  >  Java  >  How to convert Java to C# time

How to convert Java to C# time

PHPz
PHPzforward
2023-04-30 16:25:071522browse

In Java, you can use System.currentTimeMillis() to obtain the long form of the current time. Its notation form is the number of milliseconds from January 1, 1970 to the current time. Web Service is written in Java. This long data is converted into timestamp and then stored in MySQL, so this value can be passed in directly when calling our Web Service.
But the way to calculate time under .NET is different. The calculation unit is Ticks. Here you need to do a C# time conversion. Regarding Ticks, msdn says this:
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, January 1, 0001.
is the ten millionth of a second from midnight on January 1, the first year of the Christian era to the specified time. In order to compare with Java, it is expressed as one ten thousandth of a millisecond.
(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 do Java and C# time conversion by writing it as above, or you can also write it like this:

TimeSpan ts=new TimeSpan( System.DateTime.UtcNow.Ticks - new DateTime(1970, 1, 1, 0, 0, 0).Ticks);   (long)ts.TotalMilliseconds;

It should be noted that System.DateTime.UtcNow is used here instead of System.DateTime.Now, because We are in Dongba District. If you use the latter method, you will find that the time is 8 hours different from what you imagined. Java and C# time conversion is completely realized here.

The above is the detailed content of How to convert Java to C# time. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete