nanoseconds to seconds

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 2368 Views - Last Post: 07 May 2013 - 05:13 PM Rate Topic: -----

#1 psh  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 25-March 13

nanoseconds to seconds

Posted 05 May 2013 - 03:07 PM

i tried this but not working i get different result everytime 2 ,3 seconds

  public static void main(String[] args) throws InterruptedException {
        
        long y = System.nanoTime() ;
        Thread.sleep(3000);
        long x = System.nanoTime();
        long elapsedTime = x- y;
        double seconds = (double)elapsedTime / 1000000000.0;
        System.out.println((int)seconds);


    }



This post has been edited by psh: 05 May 2013 - 03:11 PM


Is This A Good Question/Topic? 0
  • +

Replies To: nanoseconds to seconds

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: nanoseconds to seconds

Posted 05 May 2013 - 03:36 PM

Why do you process them as double ?

If something is really true is that a nano second, which is such precise, really does not need a fractional part... and your computer won't be able to give it any way so ?
You are attacking a false problem...

Depending of your CPU and OS System.nanoTime(); may return a valid value but I really but really don't see how you can try to make a double out of it !!!
Was This Post Helpful? 1
  • +
  • -

#3 psh  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 25-March 13

Re: nanoseconds to seconds

Posted 05 May 2013 - 03:39 PM

Ok i want to calculate the time that a thread sleeps in seconds ..should i use System.currentTimeMillis();

This post has been edited by GunnerInc: 07 May 2013 - 09:30 PM
Reason for edit:: Quote be gone

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: nanoseconds to seconds

Posted 05 May 2013 - 03:43 PM

This

		long y = System.nanoTime() ;
		for(int i = 0; i < 10; ++i) {
			Thread.sleep(3000);
			long x = System.nanoTime();
			long elapsedTime = x- y;
			System.out.println("Diff in nano = " + elapsedTime);
			y = x;
		}


gives me values quite respectable

Diff in nano = 2999966616
Diff in nano = 3000170064
Diff in nano = 3000181587
Diff in nano = 3000176000
Diff in nano = 3000133187
Diff in nano = 3000189131
Diff in nano = 3000170901
Diff in nano = 3000150019
Diff in nano = 3000198629
Diff in nano = 3000152952
Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: nanoseconds to seconds

Posted 05 May 2013 - 03:49 PM

System.currentTimeMillis();
is refreshed every 16.6666 milliseconds... so every 16666666 nano so a lot less precise as you seem to wish
Was This Post Helpful? 1
  • +
  • -

#6 psh  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 25-March 13

Re: nanoseconds to seconds

Posted 05 May 2013 - 03:51 PM

View Postpbl, on 05 May 2013 - 10:43 PM, said:

This

		long y = System.nanoTime() ;
		for(int i = 0; i < 10; ++i) {
			Thread.sleep(3000);
			long x = System.nanoTime();
			long elapsedTime = x- y;
			System.out.println("Diff in nano = " + elapsedTime);
			y = x;
		}


gives me values quite respectable

Diff in nano = 2999966616
Diff in nano = 3000170064
Diff in nano = 3000181587
Diff in nano = 3000176000
Diff in nano = 3000133187
Diff in nano = 3000189131
Diff in nano = 3000170901
Diff in nano = 3000150019
Diff in nano = 3000198629
Diff in nano = 3000152952


Thanks but after i have to abstract elapsed time from my current time .For example 22:33.. so i have to convert elapsed time in seconds
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: nanoseconds to seconds

Posted 05 May 2013 - 06:23 PM

View Postpsh, on 05 May 2013 - 06:51 PM, said:

Thanks but after i have to abstract elapsed time from my current time .For example 22:33..

and this 22:33.. is the representation of what ?

P.S.
I was afraid to ask you the right question because that last time about Client/Server I got a -1
Was This Post Helpful? 1
  • +
  • -

#8 psh  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 25-March 13

Re: nanoseconds to seconds

Posted 06 May 2013 - 03:03 AM

Haha..sry
Well 22:30:22 would be an example of my current time.


Client: Requests time
Server: sleeps for 2 seconds
Server: Responds my time is 22:30:28
Client: Calculates the delay and corrects the time he received from server

It would be a string and after i will split it and cast to int hours,minutes,seconds..would you think this would be a good aproach to my problem?



 public static void main(String[] args) {
        
        String time = "22:33:21" ;
        System.out.println(time);
        String array[] = time.split(":"); 
        
        int hours = Integer.parseInt(array[0]);
        int minutes = Integer.parseInt(array[1]);
        int seconds = Integer.parseInt(array[2]);
        
        int delay  = 2; // this would be a result from nanoTime
        seconds = seconds - delay; // for example if have 2 seconds delay 
        
        
        String h = String.valueOf(hours+":");
        String m = String.valueOf(minutes+":");
        String s = String.valueOf(seconds);
        
        String finalTime = h+m+s;
        System.out.println( h + m + s);
    
}


This post has been edited by GunnerInc: 07 May 2013 - 09:30 PM
Reason for edit:: Quote be gone

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: nanoseconds to seconds

Posted 06 May 2013 - 03:47 AM

The time in nano seconds returned by the system is by definition

The value returned represents nanoseconds since some fixed but arbitrary time (perhaps in the future, so values may be negative). This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees are made about how frequently values change. Differences in successive calls that span greater than approximately 292 years (263 nanoseconds) will not accurately compute elapsed time due to numerical overflow


So you just can't just relate an arbitrary hh:mm:ss to a time in nano. Also, if you increment your second value, why do you need nano precision ?
Was This Post Helpful? 1
  • +
  • -

#10 psh  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 25-March 13

Re: nanoseconds to seconds

Posted 06 May 2013 - 05:26 AM

Aha..but if i dont use nano should i use currentMillis?( im just searching what i have to use for my problem ..)

This post has been edited by GunnerInc: 07 May 2013 - 09:29 PM
Reason for edit:: Quote be gone

Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: nanoseconds to seconds

Posted 06 May 2013 - 07:24 PM

You never posted what your problem was :)

Despite the fact that your mentionned in at least 3 post "fix my problem"
but you still haven't explain it
Was This Post Helpful? 1
  • +
  • -

#12 psh  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 25-March 13

Re: nanoseconds to seconds

Posted 07 May 2013 - 06:28 AM

Im sorry maybe my english is too bad..I have one Client and one Server .

Client: Sends TIME REQUEST TO SERVER
Server: Gets the request , sleeps for a random time and after RESPONDS.
Client: Gets the respond ,now he must calculate the delay( the time that server sleeps)
Client: Calculates the delay and changes the time received from the server
ex. 22:33:23 we have delay 2 seconds so it would be 22:33:21
What should i use to calculate the delay..System.currentMilis?

This post has been edited by GunnerInc: 07 May 2013 - 09:29 PM
Reason for edit:: Quote be gone

Was This Post Helpful? 0
  • +
  • -

#13 hendrayoga  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-May 13

Re: nanoseconds to seconds

Posted 07 May 2013 - 06:32 AM

Good Job, !!!! :offtopic:/> :offtopic:/> :offtopic:/> :shuriken:/>

This post has been edited by GunnerInc: 07 May 2013 - 09:29 PM
Reason for edit:: Quote be gone

Was This Post Helpful? 0
  • +
  • -

#14 psh  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 25-March 13

Re: nanoseconds to seconds

Posted 07 May 2013 - 06:48 AM

:stupid:

This post has been edited by GunnerInc: 07 May 2013 - 09:28 PM
Reason for edit:: Quote be gone

Was This Post Helpful? 0
  • +
  • -

#15 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2557
  • View blog
  • Posts: 10,672
  • Joined: 20-September 08

Re: nanoseconds to seconds

Posted 07 May 2013 - 08:48 AM

Quote

Im sorry maybe my english is too bad..I have one Client and one Server .

Client: Sends TIME REQUEST TO SERVER
Server: Gets the request , sleeps for a random time and after RESPONDS.
Client: Gets the respond ,now he must calculate the delay( the time that server sleeps)
Client: Calculates the delay and changes the time received from the server
ex. 22:33:23 we have delay 2 seconds so it would be 22:33:21

Only the server can calculate the proper delay accurately, else the figure would be skewed by network round trips
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2