Java System.currentTimeMillis () equivalent in C #

What is the Java equivalent of System.currentTimeMillis() in C #?

+71
java c # datetime
Nov 14 '08 at 14:30
source share
10 answers

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; } 
+83
Nov 14 '08 at 14:40
source share

A common idiom in Java is using currentTimeMillis() for synchronization or scheduling purposes, where you have not been interested in the actual milliseconds since 1970, but instead calculate some relative value and compare subsequent calls to currentTimeMillis() with that value.

If this is what you are looking for, the equivalent of C # Environment.TickCount .

+61
Apr 18 '11 at 12:56
source share

We can also pretend a little and do it as an extension method so that it hangs with the DateTime class:

 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); } } 
+8
Nov 14 '08 at 15:08
source share

If you are interested in TIMING, add a link to System.Diagnostics and use a stopwatch.

For example:

 var sw = Stopwatch.StartNew(); ... var elapsedStage1 = sw.ElapsedMilliseconds; ... var elapsedStage2 = sw.ElapsedMilliseconds; ... sw.Stop(); 
+8
May 31 '11 at 22:49
source share

System.currentTimeMillis() in java returns current time in milliseconds since 1/1/1970

C # which will

 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: did utc as suggested :)

+6
Nov 14 '08 at 14:44
source share

Here is an easy way to approximate a Unix timestamp. Using UTC is closer to the unix concept, and you need to hide from double to 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 
+4
Nov 14 '08 at 14:52
source share

The structure does not include old seconds (or milliseconds) since 1970. The closest you get is DateTime.Ticks, whose number is 100 nanoseconds from January 1, 0001.

+3
Nov 14 '08 at 14:38
source share

I am just looking at the simplest way to achieve what you were aiming for, as follows:

 DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond 
+3
Nov 25 '14 at 11:02
source share

I know that the question asks the equivalent , but since I use these 2 for the same tasks, I throw GetTickCount . I might be nostalgic, but System.currentTimeMillis () and GetTickCount () are the only ones I use to get ticks.

 [DllImport("kernel32.dll")] static extern uint GetTickCount(); // call uint ticks = GetTickCount(); 
+1
Feb 26 '13 at 16:01
source share

If you want to compare the timestamp between different processes, different languages ​​(Java, C, C #), in GNU / Linux and Windows (at least seven):

FROM#:

 private static long nanoTime() { long nano = 10000L * Stopwatch.GetTimestamp(); nano /= TimeSpan.TicksPerMillisecond; nano *= 100L; return nano; } 

Java:

 java.lang.System.nanoTime(); 

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:

 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; } 
+1
May 23 '17 at 13:59
source share



All Articles