f



Getting millisecond-precision time out of a Time object?

Is there any way to get a millisecond-precision time (hh:mm:ss.sss)
out of a Time object?
Time class's toString() method only returns the time at one-second 
precision (hh:mm:ss), which is not accurate enough for my purposes.

-- 
-Aki "Sus" Laukkanen

0
11/3/2004 3:48:21 PM
comp.lang.java.help 13340 articles. 0 followers. Post Follow

10 Replies
591 Views

Similar Articles

[PageSpeed] 6

On Wed, 03 Nov 2004 17:48:21 +0200, Aki "Sus" Laukkanen wrote:

> Is there any way to get a millisecond-precision time (hh:mm:ss.sss)
> out of a Time object?

...errr.  Are you referring to java.sql.Time?

It's super-class, java.util.Date, has getTime() that provides msec.

HTH

-- 
Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane
0
SeeMySites (5478)
11/3/2004 4:06:15 PM
> which is not accurate enough for my purposes.

I don't know how deep you want to go into precision, but just a hint:
Windows system time quant is 50ms, this is the smallest quant you can get
any *real* results. Below that you are getting the bogus = (myTime mod 50) +
a spice from the floating part of the number (to do not show all zeros at
the end every time).

I don't know about the quant on Unix and Mac, but is also *definitely* not
1ms and of course not 1ns

(And I saw some guys seriously extending default stuff to work with
picosecs, which is really... sad).



0
schools_ring (242)
11/3/2004 5:09:56 PM
On Wed, 3 Nov 2004 18:09:56 +0100, VK wrote:

> (And I saw some guys seriously extending default stuff to work with
> picosecs, which is really... sad).

Or intensely funny, ..depending on how you look at it.   ;-)

-- 
Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane
0
SeeMySites (5478)
11/3/2004 5:20:37 PM
Andrew Thompson <SeeMySites@www.invalid> wrote:
> On Wed, 03 Nov 2004 17:48:21 +0200, Aki "Sus" Laukkanen wrote:
> 
>> Is there any way to get a millisecond-precision time (hh:mm:ss.sss)
>> out of a Time object?
> 
> ..errr.  Are you referring to java.sql.Time?
> 
> It's super-class, java.util.Date, has getTime() that provides msec.

And whether or not it is truly accurate to milliseconds depends on the
host environment. So check that as well.


-- 
Oscar Kind                                    http://home.hccnet.nl/okind/
Software Developer                    for contact information, see website

PGP Key fingerprint:    91F3 6C72 F465 5E98 C246  61D9 2C32 8E24 097B B4E2
0
oscar1 (512)
11/3/2004 5:58:09 PM
VK wrote:

> > which is not accurate enough for my purposes.
>
> I don't know how deep you want to go into precision, but just a hint:
> Windows system time quant is 50ms, this is the smallest quant you can get
> any *real* results. Below that you are getting the bogus = (myTime mod 50) +
> a spice from the floating part of the number (to do not show all zeros at
> the end every time).

The accuracy of Windows 95/98 is 55ms, the accuracy of Windows NT4, 2000 and XP
is 10ms (single CPU) or 15.625ms (dual CPU).

<url: http://mindprod.com/jgloss/time.html />

Other references that refer to similar numbers (10 - 15ms accuracy on Windows
NT):

<url: http://www.cl.cam.ac.uk/users/kw217/useful/win32time.html#timingwin />
<url: http://www.microsoft.com/whdc/system/CEC/mm-timer.mspx#EEAA />
<url:
http://discuss.fogcreek.com/joelonsoftware/default.asp?cmd=show&ixPost=85520 />

It appears there are more accurate timers available in Windows, but Java
probably doesn't have access to them:

<url:
http://msdn.microsoft.com/library/en-us/multimed/htm/_win32_timer_resolution.asp
/>
<url: http://www.microsoft.com/whdc/system/CEC/mm-timer.mspx#EDAA />

--
Grant Wagner <gwagner@agricoreunited.com>

0
gwagner (128)
11/3/2004 9:15:23 PM
Grant Wagner wrote:
> It appears there are more accurate timers available in Windows, but Java
> probably doesn't have access to them:

More precise timing has been added to Java 1.5: System.nanoTime(). Of 
course, if the platform doesn't deliver the required precision or 
resolution that method won't help, too.

/Thomas
0
nobody89 (1641)
11/4/2004 1:02:17 PM
Andrew Thompson wrote:

> On Wed, 03 Nov 2004 17:48:21 +0200, Aki "Sus" Laukkanen wrote:
> 
> 
>>Is there any way to get a millisecond-precision time (hh:mm:ss.sss)
>>out of a Time object?
> 
> 
> ..errr.  Are you referring to java.sql.Time?
> 
> It's super-class, java.util.Date, has getTime() that provides msec.

Not the way I want them.
It just tells how many milliseconds after 0:00:00.000 1. jan., 1970 it 
was initialized. What it does not tell is at what time (down to Windows 
time resolution) *today* it was initialized.
I'd need the [as] precise [as possible] start time for my program, 
preferably in the aforementioned hh:mm:ss.sss format.
Both java.sql.Time and java.util.Date seem to give out just either the 
wanted information (thw time of the day) at insufficient resolution, or 
a very large number of milliseconds that has passed since the beginning 
of the 70's.
Is there any kind of a work-around for this?

-- 
-Aki "Sus" Laukkanen

0
11/4/2004 2:33:05 PM
"Aki "Sus" Laukkanen" <aki.laukkanenREMOVETHIS@helsinki.fi> wrote in message
news:cmdej3$s16$1@oravannahka.helsinki.fi...

> Not the way I want them.
> It just tells how many milliseconds after 0:00:00.000 1. jan., 1970 it
> was initialized. What it does not tell is at what time (down to Windows
> time resolution) *today* it was initialized.
> I'd need the [as] precise [as possible] start time for my program,
> preferably in the aforementioned hh:mm:ss.sss format.
> Both java.sql.Time and java.util.Date seem to give out just either the
> wanted information (thw time of the day) at insufficient resolution, or
> a very large number of milliseconds that has passed since the beginning
> of the 70's.
> Is there any kind of a work-around for this?

There is an 'around the barn' way you might try - it's a fair amount
of work but straightforward...

Get the current date and extract the year, month, day, hour, minute, and
second.  Then use the Calendar.set() method to construct a new
calendar object that has these attributes.

Then use getTimeInMillis() to get the value from your constructed
Calendar object, which will correspond to hh:mm:ss.000. Subtract
that value from the original current time in milliseconds.

Then build up your own formatted string object that looks like
hh:mm:ss:milliseconds


0
11/4/2004 3:38:23 PM
mark.johnson@lsil.com wrote:

> There is an 'around the barn' way you might try - it's a fair amount
> of work but straightforward...
> 
> Get the current date and extract the year, month, day, hour, minute, and
> second.  Then use the Calendar.set() method to construct a new
> calendar object that has these attributes.
> 
> Then use getTimeInMillis() to get the value from your constructed
> Calendar object, which will correspond to hh:mm:ss.000. Subtract
> that value from the original current time in milliseconds.
> 
> Then build up your own formatted string object that looks like
> hh:mm:ss:milliseconds

Managed to "hack" it the way you suggested. :)
It's an ugly way, but it works. I was thinking along the same lines 
already, but wasn't quite sure how to do that.

Thanks a lot for your help. :)

-- 
-Aki "Sus" Laukkanen

0
11/4/2004 5:12:28 PM
mark.johnson@lsil.com wrote:
> Get the current date and extract the year, month, day, hour, minute, and
> second.  Then use the Calendar.set() method to construct a new
> calendar object that has these attributes.
> 
> Then use getTimeInMillis() to get the value from your constructed
> Calendar object, which will correspond to hh:mm:ss.000. Subtract
> that value from the original current time in milliseconds.
> 
> Then build up your own formatted string object that looks like
> hh:mm:ss:milliseconds

Why not just something like this?

<sscce>
import java.util.Calendar;
import java.text.SimpleDateFormat;

public class JavaTest
{
	private static final SimpleDateformat TIME =
		new SimpleDateFormat("HH:mm:ss:SSS");

	public static void main(String[] args)
	{
		System.out.println(TIME.format(new Date()));
	}
}
</sscce>

Seems easier to me...


-- 
Oscar Kind                                    http://home.hccnet.nl/okind/
Software Developer                    for contact information, see website

PGP Key fingerprint:    91F3 6C72 F465 5E98 C246  61D9 2C32 8E24 097B B4E2
0
oscar1 (512)
11/4/2004 5:32:37 PM
Reply:

Similar Artilces:

time in milliseconds by calling time.time()
I am trying to measure some system response time by using the time.time () or time.clock() in my script. However, the numbers I get are in 10s of milliseconds. For example, 1248481670.34 #from time.time() 0.08 #from time.clock() That won't work for me, since the response time may be only a few milliseconds. My environment is Solaris 10 with Python 2.4.4 (#7, Feb 9 2007, 22:10:21). SunOS 5.10 Generic_137112-07 i86pc i386 i86pc The tricky thing is, if I run the python interpreter and import the time module, I can get a time floating number in better precision by cal...

time.clock() or time.time()
What's the difference between time.clock() and time.time() (and please don't say clock() is the CPU clock and time() is the actual time because that doesn't help me at all :) I'm trying to benchmark some function calls for Zope project and when I use t0=time.clock(); foo(); print time.clock()-t0 I get much smaller values than when I use time.clock() (most of them 0.0 but some 0.01) When I use time.time() I get values like 0.0133562088013, 0.00669002532959 etc. To me it looks like time.time() gives a better measure (at least from a statistical practical point of view). peterbe@gmail.com wrote: > What's the difference between time.clock() and time.time() > (and please don't say clock() is the CPU clock and time() is the actual > time because that doesn't help me at all :) > > I'm trying to benchmark some function calls for Zope project and when I > use t0=time.clock(); foo(); print time.clock()-t0 > I get much smaller values than when I use time.clock() (most of them > 0.0 but some 0.01) > When I use time.time() I get values like 0.0133562088013, > 0.00669002532959 etc. > To me it looks like time.time() gives a better measure (at least from a > statistical practical point of view). time.time() is very likely what you want. time.time() measures real time, while time.clock() measures the time the CPU dedicates to your program. For example, if your function spends 2 seconds doing pure I/O and 3 seconds do...

time.time or time.clock
I'm having some cross platform issues with timing loops. It seems time.time is better for some computers/platforms and time.clock others, but it's not always clear which, so I came up with the following to try to determine which. import time # Determine if time.time is better than time.clock # The one with better resolution should be lower. if time.clock() - time.clock() < time.time() - time.time(): clock = time.clock else: clock = time.time Will this work most of the time, or is there something better? Ron On Jan 14, 7:05 am, Ron Ad...

time.time()
am I doing this wrong: print (time.time() / 60) / 60 #time.time has been running for many hours if time.time() was (21600/60) then that would equal 360/60 which would be 6, but I'm not getting 6 so I'm not doing the division right, any tips? On Sat, 24 Jan 2004 13:01:40 -0500, Bart Nessux <bart_nessux@hotmail.com> wrote: >am I doing this wrong: > >print (time.time() / 60) / 60 #time.time has been running for many hours > >if time.time() was (21600/60) then that would equal 360/60 which would >be 6, but I'm not getting 6 so I'm not doing the divisi...

Is time.time() < time.time() always true?
So, I was blazin' some mad chronix, as they say, and got on to thinking about Python. The question was, is the statement: time.time() < time.time() always true? Seems it should be false, since the statement itself occurs at one time instant.. but of course we know that python doesn't execute code that way.. So my question is, why doesn't Python work this way? (PS, I wasn't smoking anything, its a figure of speech :) ) On 21 Nov 2006 15:10:25 -0800, flamesrock <flamesrock@gmail.com> wrote: > So, I was blazin' some mad chronix, as they say, and got on to th...

JAVA Time Problem: Want to get the current time and perform validation
Hi, I am working on a module which has to check the current time. If the time is within the bounds then the system performs further steps. I have to use the Eastern std. Time as the base time for the validation. Here is the sample code: TimeZone myTimeZone = TimeZone.getTimeZone("EST"); Calendar calendar = Calendar.getInstance(); int iHour = calendar.get(Calendar.HOUR_OF_DAY); System.out.print(iHour); //Time between 8 AM and 8PM EST if (iHour > 8 & iHour < 20) { ...

timing response times in milliseconds
I'm using an access xp form to present stimuli which participants in an experiment must respond to as quickly as they can. I need to time their responses as accurately as possible. At the moment I have a timer with interval set to 1 and increment a variable by 1 each timer event then compare the time of presentation with the time of the keypress. However this is very inaccurate bacuse the timer event doesn't actually happen this often, it seems to me to be quite irregular and unless I set the interval to something much slower (e.g. 500 ms) it doesn't seem to be able to keep up. It&...

How application get precise time from Win NT/XP system time that synchronies to NTP
Hi, My applications is to do time stamp that need 10 microsecond accuracy. Many PCs in network environment will need ssme precise time stamp. One way to do it is to install Trimble Acutime GPS for each PC. Because of cost consideration, I installed a Win NT NTP server that use Trimble Acutime GPS as reference clock and the rest of PCs synchronize to the server and I try to find a way to get precise time from NT/ XP system. I head NT/XP system time can only reach 10 millisecond resolution, if it is true? Or there is another way to do it. Thanks, Roger Roger Chou <rgr.chou@gmail.com> w...

RE: Is time.time() < time.time() always true?
Chris Mellon wrote: > On 21 Nov 2006 15:10:25 -0800, flamesrock <flamesrock@gmail.com> > wrote:=20 >> So, I was blazin' some mad chronix, as they say, and got on to >> thinking about Python.=20 >>=20 >> The question was, is the statement: >>=20 >> time.time() < time.time() >>=20 >> always true? Seems it should be false, since the statement itself >> occurs at one time instant.. but of course we know that python >> doesn't execute code that way.. So my question is, why doesn't >> Python work this way?=...

Re: [ntp:questions] Re: How application get precise time from Win NT/XP system time that synchronies to NTP
----- Original Message Follows ----- > Martin, > > If my application polls the system time once every tick, i.e. 10 > millisecond, can I get nanosecond resolution and accuracy that NTP can > provide? > The simple answer is no way. The more complicated answer is that you would need an real atomic clock directly connected on a machine running a realtime O/S with absolutely no other processes that the absolute essential ones. Windows wouldn't even be in the picture here, it's not designed for what you are asking. What exactly are you trying to do and why? A little ex...

Probelm to post XML data in a loop. First time XML is posted, second time data is getting truncated. Please help.
Hi, I am posting an XML document to an asp applicaiton. This XML document, i am sending in a loop. That is, if the XML document is big, I am sending the first 50 records first time, then next 50 records and so on. The problem is, I am able to send first 50, but next 50 is causing problem and the asp application is receiving the truncated XML. When I send 20 after 20, first two times the xml goes fine, but the third time the XML is truncating. Any idea what is the problem? I am using post, so the size should not be a problem. And more over the XML size is not more tha...

Getting time from NT time server.
Hi all. I'm running on VxWorks Embedded system. I want to get the current time from an NT Time Server. I have in the NET this NT and the time server is up and running. I'm using: + ansi-C code + TCP/IP (UDP) My problem is not my system. Specifically, I do not know how to connect to the NT server. Which port? Which protocol? I'm looking for the C/C++ code that do this remotely. That is, a code the generate a request. Sends it. Receives the answer, and parse it. Thanks, --Shay Look up the NTP and SNTP protocols. I believe the protocols use UDP port 123. ....Chris <shy01...

Lauching images from time to time, help
Hello, everybody! I am doing a volunteer work for estimulating senses. The basic idea is: the user enters a interval and from time to time an image or sound is launched at the monitor. The goal is to evaluate their ability to understand, recognize, and react to some stimuli. If anybody could send me any sample of code to help me i will very appreciate it. thanks in advance! regards Leonardo "Quem espera sempre alcanca, se trabalha enquanto espera" ...

Getting the Date and Time From a Time Server
Hello everyone, Is there a way to use the Import function to grab the current date and time from a time server on the Internet? Regards, Gregory Maybe you could run the "ntpdate" program external and retreive the time offset between your computer clock and the time server. (in the following example, the time server is only good in canada) Run["ntpdate -q time.chu.nrc.ca >/tmp/toto"] res=Import["/tmp/toto", "Table","FieldSeparators" -> {" ", ","}] offset=res[[1,6]] (* not very portable... :-( *...

Time Shifting, time scaling.... help!!!
%% t = -10 : 0.001 : 10; x = 0.*(t<-2) + (-4-(2.*t)).*(t>=-2 & t<0) + (-4+(3.*t)).*(t>=0 & t<4) + (16-(2.*t)).*(t>=4 & t<8) + 2.*(t>=8); hold on plot(t, x, 'r') plot(t+1, x.*3, 'c') plot(4.*t, x./2, '--m') plot((t-1)./2, -2.*x, ':b') grid on hold off %% I have to plot 3x(t +1), (1/2) x(4t), -2x((t-1)/2) Amplitude and Time scaling is correct (do tell me if anything is wrong) here but Time scaling is always wrong. Could you help me in plotting with correct time scaling ? I wish to know if there is shorter wa...

time manipulation getting invalid times
I have a script that deals with time in hours, minures, seconds and milliconds. Initially I was using the round function from Math::Round It was working fine then I noticed I was getting an error occasionaly. Turned out it was when the seconds were 59 and the milliseconds were above 500, I was rounding the minutes from 59 to 60 yeilding invalid times like 13 minutes and 60 seconds instead of 14 minutes and 00 seconds. Example: HH:MM:SS,miliseconds $lines[0] = 00:13:59,934 my $roundedSeconds = sprintf("%02d",(round(substr($lines[0],-6)))); Using the round function from Math...

How do I get time in another time zone ??
I'm in the UK using British Summer Time (BST). I'd like to get the time in New York (EDT). I believe there is an option to 'clock' to change the time zone, but any combination I try gives the wrong result. This is what I have: set foobar [clock format [clock seconds]] Ideally, I'd like to get the time in this format: Sun Oct 19 17:38:07 EDT (New York time) I can obviously add '(New York time)'. Any suggestions how I can get the time in the EDT timezone, and ideally in the format shown. dave Dave wrote: > I'm in the UK using British Summer Ti...

Time Machine/Time Capsule help?
Since I have a Time Capsule, why is Time Machine cluttering my hard drive with what ought to be the same info and keeping me from installing something new? I can't see any way to access Time Capsule except via Time Machine, and I'm about ready to give up on backup if it's only going to block me from adding anything new on the laptop. -- Erilar, biblioholic medievalist In article <drache-CF7F3F.11003613092012@news.eternal-september.org>, erilar <drache@chibardun.net.invalid> wrote: > Since I have a Time Capsule, why is Time Machine clutte...

Time zone objects and time change
Looking at time zone object QN0800U on our V5R3 system, it says that daylight saving time ends on the last Sunday in October. However, I think it actually ends on the first Sunday in November this year. Is there a way to refresh the time zone objects so they show correct stop and start dates for DST? Jonathan Ball wrote: > Looking at time zone object QN0800U on our V5R3 system, it says that > daylight saving time ends on the last Sunday in October. However, I > think it actually ends on the first Sunday in November this year. > > Is there a way to refresh the time zone...

Get time event at a specific time
Hi! Is it possible to get a time event at a specific time, for instance eight a'clock? My program is running in the background and is minimized to the tray bar. If not, is there a smooth way to accomplish this in a different way? Best regards, Andreas Lundgren "Andreas" <d99alu@efd.lth.se> wrote in message news:f7cd4365.0310022357.75ec4f66@posting.google.com... > Hi! > > Is it possible to get a time event at a specific time, for instance > eight a'clock? My program is running in the background and is > minimized to the tray bar. > > If not, is ...

getting time in milliseconds
Hi , can anyone tell me how to find time in milliseconds on mac os . Is there any function in standard library for same . Regards Mangesh . mangesh wrote: > Hi , > can anyone tell me how to find time in milliseconds on mac os . Is > there any function in standard library for same . There is a C++ standard function called time() but it only has second resolution. If you mean mac os x there should be gettimeofday() which can handle microsecond resolution and is POSIX (try: man gettimeofday). Markus Schoder wrote: > mangesh wrote: > > Hi , > > can anyone tell me h...

Get a timing in milliseconds
Hi, Is there a way to time the execution of a process in milliseconds with GT.M ? Kind of like: new m1, m2 set m1 = $$timeInMillisecondsNow ;... processing set m2 = $$timieInMillisecondsNow new r set r=m2-m1 write "it took ",r," millisecs to complete.",! Thanks. SR hi, S´┐Żbastien de Mapias wrote: > Is there a way to time the execution of a process in milliseconds > with GT.M ? > Kind of like: > new m1, m2 > set m1 = $$timeInMillisecondsNow > ;... processing > set m2 = $$timieInMillisecondsNow > new r > set r=m2-m1 > write "it took "...

Get time in milliseconds
Hi: I am developing a16 bits OS for a PC, and I need get the time including millseconds. The Interrupt 1A provide only HH MM SS. Exist any RTC in PC architecture that can sayme the actual time in milliseconds Thanks in advance. Att. Diego Cueva "Diego Cueva" <spamtrap@crayne.org> wrote: > >I am developing a16 bits OS for a PC, and I need get the time including >millseconds. >The Interrupt 1A provide only HH MM SS. > >Exist any RTC in PC architecture that can sayme the actual time in >milliseconds What do you mean by "actual time"? The rea...

Getting time in milliseconds
Hi: is there a way to get current system time in milliseconds... which functions and headers?? thanks pravesh "Pravesh" <praveshsubramanian@yahoo.com> wrote in message news:e5a50c3b.0411081532.5ad3177f@posting.google.com... > Hi: > is there a way to get current system time in milliseconds... Not portably. > which functions and headers?? The standard functions for retrieving system time and working with time are declared by the header <time.h>. They including 'time()', 'localtime()', and 'gmtime()', among others. However, the resoluti...

Web resources about - Getting millisecond-precision time out of a Time object? - comp.lang.java.help

Stopwatch - A professional and accurate stopwatch with milliseconds precision on the App Store on iTunes ...
Get Stopwatch - A professional and accurate stopwatch with milliseconds precision on the App Store. See screenshots and ratings, and read customer ...

Resources last updated: 3/9/2016 10:19:06 PM