473,728 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Java System.currentT imeMillis() equivalent

Does anyone know what the C# equivalent of System.currentT imeMillis()
would be?

Nov 1 '06 #1
9 33095
System.DateTime .Now.Millisecon d

<Az******@gmail .comwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.com.. .
Does anyone know what the C# equivalent of System.currentT imeMillis()
would be?

Nov 1 '06 #2
I think what you want is:

DateTime.Now.Ti cks
Nov 1 '06 #3
And if you divide the number from Ticks by 10000 you get milliseconds.

so for example if what you wanted to do was time something, you could do:

long ticks = DateTime.Now.Ti cks;
System.Threadin g.Thread.Sleep( 20000);
Console.WriteLi ne((DateTime.No w.Ticks - ticks)/10000);

and it will correctly rprint 20000 (maybe plus a few milliseconds because of
the brief time it took to do the actual console.writeli ne)
Nov 1 '06 #4
no, this just returns the millisecond field of the current date time.

fallenidol wrote:
System.DateTime .Now.Millisecon d

<Az******@gmail .comwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.com.. .
Does anyone know what the C# equivalent of System.currentT imeMillis()
would be?
Nov 1 '06 #5
ah sorry. i was just guessing what currentTimeMill is did

<Az******@gmail .comwrote in message
news:11******** **************@ e3g2000cwe.goog legroups.com...
no, this just returns the millisecond field of the current date time.

fallenidol wrote:
>System.DateTim e.Now.Milliseco nd

<Az******@gmai l.comwrote in message
news:11******* *************** @m73g2000cwd.go oglegroups.com. ..
Does anyone know what the C# equivalent of System.currentT imeMillis()
would be?

Nov 1 '06 #6
MrNobody <Mr******@discu ssions.microsof t.comwrote:
And if you divide the number from Ticks by 10000 you get milliseconds.

so for example if what you wanted to do was time something, you could do:

long ticks = DateTime.Now.Ti cks;
System.Threadin g.Thread.Sleep( 20000);
Console.WriteLi ne((DateTime.No w.Ticks - ticks)/10000);

and it will correctly rprint 20000 (maybe plus a few milliseconds because of
the brief time it took to do the actual console.writeli ne)
However, you need to do extra translation to actually give the
equivalent of System.currentT imeMillis, as that is:

a) based on UTC
b) Since 1970, not 1AD

Here's an equivalent (tested by running a Java program and the C#
equivalent in close proximity - not tested for timezones as I'm in
GMT...)
static readonly DateTime Epoch = new DateTime (1970, 1, 1);
static long CurrentTimeMill is()
{
return (long)(DateTime .UtcNow-Epoch).TotalMil liseconds;
}

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 1 '06 #7

"MrNobody" wrote...
>I think what you want is:

DateTime.Now.Ti cks
But it's not that simple...

There's at least three things to consider here:

1. DateTime.Ticks is in *100ds of nanoseconds*, not milliseconds.

2. A major difference between the Java way and the .NET way of handling
dates is that DateTime.Now yields a *local* date, while the Java Date
internally is a UTC date. This means that you want to use the
DateTime.UtcNow .Ticks instead.

3. But this isn't enough to compare with the milliseconds in the Java Date,
as Java and .NET uses different epochs; Java uses 1970-01-01, while .NET
uses 0001-01-01
/// Bjorn A


Nov 1 '06 #8
thanks guys,

I kind of combined both ideas and it seems to work.

DateTime UtcNow = DateTime.UtcNow .Ticks;
DateTime baseTime = new DateTime(1970, 1, 1, 0, 0, 0);
long timeStamp = (UtcNow - baseTime).Ticks / 10000;
Bjorn Abelli wrote:
"MrNobody" wrote...
I think what you want is:

DateTime.Now.Ti cks

But it's not that simple...

There's at least three things to consider here:

1. DateTime.Ticks is in *100ds of nanoseconds*, not milliseconds.

2. A major difference between the Java way and the .NET way of handling
dates is that DateTime.Now yields a *local* date, while the Java Date
internally is a UTC date. This means that you want to use the
DateTime.UtcNow .Ticks instead.

3. But this isn't enough to compare with the milliseconds in the Java Date,
as Java and .NET uses different epochs; Java uses 1970-01-01, while .NET
uses 0001-01-01
/// Bjorn A
Nov 2 '06 #9
<Az******@gmail .comwrote:
I kind of combined both ideas and it seems to work.

DateTime UtcNow = DateTime.UtcNow .Ticks;
DateTime baseTime = new DateTime(1970, 1, 1, 0, 0, 0);
long timeStamp = (UtcNow - baseTime).Ticks / 10000;
Why rely on a constant which isn't terribly obvious when the
TotalMillisecon ds makes it absolutely clear what the units returned
are?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 2 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2149
by: pawel | last post by:
I have made some comparision C# to Java RegularExpression. The problem was to find out if the rule match some text. Matching were done for precompiled regular expressions, in 100000 iterations loop. Those loops were executed 11 times and average value of consumend time was calculated. Below are codes for both classes. And I found, that Java implementation is 2 to 5 times faster than C# (it depends on complexity of expression). Maybe my...
14
1613
by: Lloyd Dupont | last post by:
here a little test program. it's so simple taht it should deliver simmilar result. it's also so simple that people that might says it's unfair to the looser (why Java people are always arguing like that ?)... anyway see for yourself the test command lines: javac SimplePerf.java && java SimplePerf 10000000 csc /nologo SimplePerf.cs && SimplePerf 10000000
1
5416
by: neoedmund | last post by:
/** * Returns the current time in milliseconds. Note that * while the unit of time of the return value is a millisecond, * the granularity of the value depends on the underlying * operating system and may be larger. For example, many * operating systems measure time in units of tens of * milliseconds. * * <pSee the description of the class <code>Date</codefor * a discussion of slight discrepancies that may arise between
14
5686
by: mlw | last post by:
Do not take anything about this, it is not a flame or troll, while I'm not new to Java I favor C++. However, I may need to use it in a contract position, and am concerned that the restrictions it places on application code. Take, for instance, this C++ construct: class foo { char *m_name;
6
10269
by: tak | last post by:
Hi. I have a client / server application, which the client will send the server a timestamp everytime when there is a transaction. The client is using Java, and it sends the timestamp using java's System.currentTimeMillis(), which returns, "the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC." On the server side - it is implemented in C++, so, with this number of
2
2771
by: sdanda | last post by:
Hi , Do you have any idea how to improve my java class performance while selecting and inserting data into DB using JDBC Connectivity ......... This has to work for more than 8,00,000 of records ..... Can you give some performance tips if you have known 1) For this I am using oci driver ( because I m using oracle 10g) instead of thin driver 2) In that programme I m using prepared statement instead of statement 3) I am...
350
11793
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use one, and in what percentage of your projects is one used? I have never used one in personal or professional C++ programming. Am I a holdover to days gone by?
318
11039
by: King Raz | last post by:
The shootout site has benchmarks comparing different languages. It includes C# Mono vs Java but not C# .NET vs Java. So I went through all the benchmark on the site ... http://kingrazi.blogspot.com/2008/05/shootout-c-net-vs-java-benchmarks.html Just to keep the post on topic for my friends at comp.lang.c++, how do I play default windows sounds with C++?
7
3658
by: Sanny | last post by:
I have an app in Java. It works fine. Some people say Java works as fast as C. Is that true? C can use assembly language programs. How much faster are they inplace of calling general routines. Can C++ directly acess the Registers. Will the Computation 5-10 times faster than Java?
0
8895
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8755
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9415
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9267
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9125
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4787
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3231
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2159
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.