Java System.currentTimeMillis() 在 C# 中等效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/290227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java System.currentTimeMillis() equivalent in C#
提问by
What is the equivalent of Java's System.currentTimeMillis()
in C#?
System.currentTimeMillis()
C#中Java的等价物是什么?
回答by Cristian Libardo
The framework doesn't include the old seconds (or milliseconds) since 1970. The closest you get is DateTime.Ticks which is the number of 100-nanoseconds since january 1st 0001.
该框架不包括自 1970 年以来的旧秒(或毫秒)。最接近的是 DateTime.Ticks,它是自 0001 年 1 月 1 日以来的 100 纳秒数。
回答by Jon Skeet
An alternative:
替代:
private static readonly DateTime Jan1st1970 = new DateTime
(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static long CurrentTimeMillis()
{
return (long) (DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
}
回答by Hath
the System.currentTimeMillis()
in java returns the current time in milliseconds from 1/1/1970
在System.currentTimeMillis()
Java中返回从1/1/1970以毫秒为单位的当前时间
c# that would be
c# 那将是
public static double GetCurrentMilli()
{
DateTime Jan1970 = new DateTime(1970, 1, 1, 0, 0,0,DateTimeKind.Utc);
TimeSpan javaSpan = DateTime.UtcNow - Jan1970;
return javaSpan.TotalMilliseconds;
}
edit: made it utc as suggested :)
编辑:按照建议将其设为 UTC :)
回答by gimel
Here is a simple way to approximate the Unix timestamp.
Using UTC is closer to the unix concept, and you need to covert from double
to long
.
这是一种近似 Unix 时间戳的简单方法。使用UTC是接近UNIX的概念,你需要从隐蔽double
到long
。
TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
long millis = (long)ts.TotalMilliseconds;
Console.WriteLine("millis={0}", millis);
prints:
印刷:
millis=1226674125796
回答by Joel Coehoorn
We could also get a little fancy and do it as an extension method, so that it hangs off the DateTime class:
我们也可以花点心思把它作为一个扩展方法来做,这样它就挂在 DateTime 类之外:
public static class DateTimeExtensions
{
private static DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static long currentTimeMillis(this DateTime d)
{
return (long) ((DateTime.UtcNow - Jan1st1970).TotalMilliseconds);
}
}
回答by Barend
A common idiom in Java is to use the currentTimeMillis()
for timing or scheduling purposes, where you're not interested in the actual milliseconds since 1970, but instead calculate some relative value and compare later invocations of currentTimeMillis()
to that value.
Java 中的一个常见习惯用法是将currentTimeMillis()
用于计时或调度目的,您对自 1970 年以来的实际毫秒数不感兴趣,而是计算一些相对值并将以后的调用currentTimeMillis()
与该值进行比较。
If that's what you're looking for, the C# equivalent is Environment.TickCount
.
如果这就是您要查找的内容,那么 C# 等效项是Environment.TickCount
.
回答by bstabile
If you are interested in TIMING, add a reference to System.Diagnostics and use a Stopwatch.
如果您对 TIMING 感兴趣,请添加对 System.Diagnostics 的引用并使用秒表。
For example:
例如:
var sw = Stopwatch.StartNew();
...
var elapsedStage1 = sw.ElapsedMilliseconds;
...
var elapsedStage2 = sw.ElapsedMilliseconds;
...
sw.Stop();
回答by Bitterblue
I know question asks for equivalentbut since I use those 2 for the same tasks I throw in GetTickCount. I might be nostalgic but System.currentTimeMillis() and GetTickCount() are the only ones I use for getting ticks.
我知道问题要求等效,但由于我将这两个用于相同的任务,因此我将其放入GetTickCount。我可能很怀旧,但 System.currentTimeMillis() 和 GetTickCount() 是我用来获取滴答声的唯一方法。
[DllImport("kernel32.dll")]
static extern uint GetTickCount();
// call
uint ticks = GetTickCount();
回答by TechCrap
I just consider the most straight forward way how to achieve what you've been striving for as follows:
我只是考虑如何实现你一直在努力的最直接的方法如下:
DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond
回答by Aubin
If you want a timestamp to be compared between different processes, different languages (Java, C, C#), under GNU/Linux and Windows (Seven at least):
如果您想在 GNU/Linux 和 Windows(至少七种)下,在不同进程、不同语言(Java、C、C#)之间比较时间戳:
C#:
C#:
private static long nanoTime() {
long nano = 10000L * Stopwatch.GetTimestamp();
nano /= TimeSpan.TicksPerMillisecond;
nano *= 100L;
return nano;
}
Java:
爪哇:
java.lang.System.nanoTime();
C GNU/Linux:
C GNU/Linux:
static int64_t hpms_nano() {
struct timespec t;
clock_gettime( CLOCK_MONOTONIC, &t );
int64_t nano = t.tv_sec;
nano *= 1000;
nano *= 1000;
nano *= 1000;
nano += t.tv_nsec;
return nano;
}
C Windows:
C 窗口:
static int64_t hpms_nano() {
static LARGE_INTEGER ticksPerSecond;
if( ticksPerSecond.QuadPart == 0 ) {
QueryPerformanceFrequency( &ticksPerSecond );
}
LARGE_INTEGER ticks;
QueryPerformanceCounter( &ticks );
uint64_t nano = ( 1000*1000*10UL * ticks.QuadPart ) / ticksPerSecond.QuadPart;
nano *= 100UL;
return nano;
}