473,385 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Java System.currentTimeMillis() equivalent

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

Nov 1 '06 #1
9 33005
System.DateTime.Now.Millisecond

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

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

DateTime.Now.Ticks
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.Ticks;
System.Threading.Thread.Sleep(20000);
Console.WriteLine((DateTime.Now.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.writeline)
Nov 1 '06 #4
no, this just returns the millisecond field of the current date time.

fallenidol wrote:
System.DateTime.Now.Millisecond

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

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

fallenidol wrote:
>System.DateTime.Now.Millisecond

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

Nov 1 '06 #6
MrNobody <Mr******@discussions.microsoft.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.Ticks;
System.Threading.Thread.Sleep(20000);
Console.WriteLine((DateTime.Now.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.writeline)
However, you need to do extra translation to actually give the
equivalent of System.currentTimeMillis, 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 CurrentTimeMillis()
{
return (long)(DateTime.UtcNow-Epoch).TotalMilliseconds;
}

--
Jon Skeet - <sk***@pobox.com>
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.Ticks
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.Ticks

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
TotalMilliseconds makes it absolutely clear what the units returned
are?

--
Jon Skeet - <sk***@pobox.com>
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
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...
14
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...
1
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...
14
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...
6
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...
2
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...
350
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...
318
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 ... ...
7
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. ...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.