Conversion from java to c#

D

daniel li

I am working with some java/.net conversion project and I need to
convert the following method into C#.

private java.sql.Date convertDate( Calendar calendar )
{
// calendar .getTime() returns a util.Date and I need to
// construct a
// sql.Date object. So call getTime() on the util.Date
// object to return
// a long representing milliseconds.
return new java.sql.Date( calendar.getTime().getTime() );
}

Does anybody know the equivalence in C# that will return
a long representing milliseconds?
Thanks
Daniel




*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
J

Jon Skeet [C# MVP]

daniel li said:
I am working with some java/.net conversion project and I need to
convert the following method into C#.

private java.sql.Date convertDate( Calendar calendar )
{
// calendar .getTime() returns a util.Date and I need to
// construct a
// sql.Date object. So call getTime() on the util.Date
// object to return
// a long representing milliseconds.
return new java.sql.Date( calendar.getTime().getTime() );
}

Does anybody know the equivalence in C# that will return
a long representing milliseconds?

The important thing is that it's milliseconds since Jan 1st 1970. The
easiest way is to create a DateTime of Jan 1st 1970, and subtract that
from your DateTime, and then use TimeSpan.TotalMilliseconds. Don't
forget to think about timezones, of course...
 
D

daniel li

Thanks for this reply, and I still have some questions.
1 what do you mean by 'The important thing is that it's milliseconds
since Jan 1st 1970.'?
2 Could you be kindly show me some code structure?
Thanks.

daniel

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
J

Jon Skeet [C# MVP]

daniel li said:
Thanks for this reply, and I still have some questions.
1 what do you mean by 'The important thing is that it's milliseconds
since Jan 1st 1970.'?

That's what java.util.Date.getDate() returns.
2 Could you be kindly show me some code structure?

DateTime jan1970 = new DateTime (1970, 1, 1);

long millis = (long) ((myDateTime-jan1970).TotalMilliseconds);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top