convert C++ miiliseconds into Java Date
I am monitoring some events on C++ side and I get the time the event generated in milliseconds. ( DWORD evTime).
So I want to send this to a Java method in milliseconds, so that I can construct a Timestamp object.
c++ code:
// DWORD evTime is time in milliseconds
jenv->CallStaticVoidMethod(jcls, jmethID,(jlong) evTime);
java Code:
protected static void createTimeStamp(long timeAsMillisecs){
Timestamp ts = new Timestamp( timeAsMillisecs);
}
This doesn't work and I get weird dates in 1969..I believe it has to do with casting unsigned long to jlong on C++ side. Any ideas of what's wrong?
I also tried converting it into a time_t and then sending it as jlong to Java as below:
time_t iTime= evTime;
time_t rawtime = time(&iTime);
jenv->CallStaticVoidMethod(jcls, jmethID,(jlong) rawTime);
java side:
Timestamp ts = new Timestamp( timeAsMillisecs*1000);
(since time_t returns seconds, but I'm multiplying with 1000) .
This gives me correct time but it's only in seconds(eg: 2007-05-7 15:00:10.0) but I need the milliseconds. Any help is appreciated. Thanks.
[1184 byte] By [
kteegalaa] at [2007-11-27 3:37:08]

# 3
In order to get the exact number on milliseconds in C++ you can do the following:
//on the beginning of the program execution, do:
int milliSecondOffset = 0;
long time = time();
while(time() == time){}// waiting to find the exact millisecond that time() switches
milliSecondOffset = clock%1000;//calculating the offset between clock() and time()
Now you should pass to Java time() clock() and milliSecondOffset. The following Java method should return the Timestamp:
Timestamp getTimestamp(long time, int clock, int milliSecondOffset)
{
return (new Timestamp((time * 1000) + (clock%1000) ?milliSecondOffset);
}
# 6
Yes, I did think abt it. If it was up to me to create a timestamp, I would use a different way . But the problem is I can't create a timestamp by any other method..I just have to use what's given to me by the winEventProc hook function ( http://msdn2.microsoft.com/en-us/library/ms697317.aspx), which gives me the time an event is generated in milliseconds.
But I'm hoping that the rollover wouldn't be a problem, because I'll be setting the offset everytime before calling setting the event hook. There is ofcourse a chance that this wouldn't work, if :
1) The system has not been restaretdd in 49 days
2) the offset was created just a few milliseconds before the 49 day reset and the and the events start firing just after the 49-day reset.
Any other ideas are welcome though.
# 7
> But I'm hoping that the rollover wouldn't be a
> problem, because I'll be setting the offset everytime
> before calling setting the event hook. There is
> ofcourse a chance that this wouldn't work, if :
> 1) The system has not been restaretdd in 49 days
> 2) the offset was created just a few milliseconds
> before the 49 day reset and the and the events start
> firing just after the 49-day reset.
>
> Any other ideas are welcome though.
One idea is that it sounds to me like you are dealing solely with a duration and probably a short one at that.
So using Date is probably a bad idea.
Dealing with the rolloever is just a matter of comparing the start and stop values to verify that the stop value is greater than the start value.