# Calculation of delay in time

Page 1 of 1

## 11 Replies - 532 Views - Last Post: 11 May 2013 - 02:04 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=320771&amp;s=088d109d5c550af4936b2d85eb957f46&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 psh

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

# Calculation of delay in time

Posted 10 May 2013 - 08:10 PM

Maybe it sounds too easy but im really confused..
for example
```
public void calculationOfDelay() {

Calendar cal = Calendar.getInstance();
cal.getTime();

SimpleDateFormat s = new SimpleDateFormat("HH:mm:ss");
String  time = s.format(cal.getTime());

sendTime(time);
}

public void getTime() {

String time = sendTime();
// now how to calculate the delay and set the time correctly?
}

```

This post has been edited by psh: 10 May 2013 - 08:11 PM

Is This A Good Question/Topic? 0

## Replies To: Calculation of delay in time

### #2 pbl

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

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Calculation of delay in time

Posted 10 May 2013 - 08:20 PM

And we should invent the question ?
What is sendTime(); ? You haven't post that method

```public void calculationOfDelay() {

Calendar cal = Calendar.getInstance();
cal.getTime();   <--- this line is completly useless you don;t save returned value

SimpleDateFormat s = new SimpleDateFormat("HH:mm:ss");
String  time = s.format(cal.getTime());

sendTime(time);               <--- sendTime() wuith a String as parameter
Thread.sleep(2000)    <--- that can;t work without a try.catch clause
}

public void getTime() {

String time = sendTime();    <--- another sendTime() with no parameter
}

```

### #3 psh

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

## Re: Calculation of delay in time

Posted 11 May 2013 - 01:11 AM

pbl, on 11 May 2013 - 03:20 AM, said:

And we should invent the question ?
What is sendTime(); ? You haven't post that method

```public void calculationOfDelay() {

Calendar cal = Calendar.getInstance();
cal.getTime();   <--- this line is completly useless you don;t save returned value

SimpleDateFormat s = new SimpleDateFormat("HH:mm:ss");
String  time = s.format(cal.getTime());

sendTime(time);               <--- sendTime() wuith a String as parameter
Thread.sleep(2000)    <--- that can;t work without a try.catch clause
}

public void getTime() {

String time = sendTime();    <--- another sendTime() with no parameter
}

```

HAHAHA im really sorry i dont sleep well these days

```
public void sendPacket() throws InterruptedException {

// send that 5 times for more acurate results
for(int i = 0; i < 5; i++)
{
Calendar cal = Calendar.getInstance();

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String  time =sdf.format(cal.getTime());

// writer is Printwriter which is writing in the OutputStream
writer.write(time+"\n");
writer.flush();

// this is the delay for each time that sends the time
}
}

public void getPacket() {

try
{
{
// do calculation to find the delay
// in the time received , correct it and then
// print it and break

System.out.println(time);
break;
}
} catch (IOException ex) {
}
}

```

This post has been edited by psh: 11 May 2013 - 01:15 AM

### #4 GregBrannon

• D.I.C Lover

Reputation: 2246
• Posts: 5,333
• Joined: 10-September 10

## Re: Calculation of delay in time

Posted 11 May 2013 - 01:19 AM

Strike 2. What's the question? What confuses you?

### #5 psh

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

## Re: Calculation of delay in time

Posted 11 May 2013 - 01:25 AM

GregBrannon, on 11 May 2013 - 08:19 AM, said:

Strike 2. What's the question? What confuses you?

Hello..my problem is how to calculate the delay in the second method getPacket() and set the time correctly

Method sendPacket writes to OutputStream

This post has been edited by psh: 11 May 2013 - 01:27 AM

### #6 psh

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

## Re: Calculation of delay in time

Posted 11 May 2013 - 02:39 AM

What im doing wrong?i want to calculate the average in time

```        public void findAvgTime() {
String [] randomTimes= { "12:00:00","11:00:00"};

int totalHours = 0 ;
int totalMin = 0 ;
int totalSeconds = 0 ;

int counter = 0;

int avgH = 0;
int avgM = 0;
int avgS = 0;

for(int i = 0; i < randomTimes.length; i++)
{

String []array = randomTimes[i].split(":");

int hours   = Integer.parseInt(array[0]);
int minute  = Integer.parseInt(array[1]);
int seconds = Integer.parseInt(array[2]);

totalHours   = totalHours   + hours;
totalMin     = totalMin     + minute;
totalSeconds = totalSeconds + seconds;

counter++;

avgH = totalHours    / counter;
avgM = totalMin      / counter;
avgS = totalSeconds  / counter;

System.out.println(totalHours);

}
System.out.println(avgH      +":"+
avgM      +":"+
avgS );

}

```

### #7 andrewsw

• Well, I'll need some beer.

Reputation: 5006
• Posts: 19,032
• Joined: 12-December 12

## Re: Calculation of delay in time

Posted 11 May 2013 - 02:50 AM

You should explain what your code is doing, and what it should be doing, and post any errors that you receive.

However, you have declared everything as integers, so the average of 11 and 12 will be 11, not 11.5.

This post has been edited by andrewsw: 11 May 2013 - 02:51 AM

### #8 psh

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

## Re: Calculation of delay in time

Posted 11 May 2013 - 02:58 AM

andrewsw, on 11 May 2013 - 09:50 AM, said:

You should explain what your code is doing, and what it should be doing, and post any errors that you receive.

However, you have declared everything as integers, so the average of 11 and 12 will be 11, not 11.5.

Damn im stupid trying to find avg with int ..thanks man
i changed to double

My code should find the average between random times that i have in the array randomTimes[]
..i got the right result but is not in really good format .. 11.5:0.0:0.0 any ideas
how to make calculation of average time better?

### #9 GregBrannon

• D.I.C Lover

Reputation: 2246
• Posts: 5,333
• Joined: 10-September 10

## Re: Calculation of delay in time

Posted 11 May 2013 - 03:08 AM

You don't have to quote the previous poster. It's there already for everyone to see.

As for improving the output, you'll have to strip any decimal parts and convert to the next time division. 11.5 hours = 11 hours, 30 minutes, 00 seconds.

### #10 psh

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

## Re: Calculation of delay in time

Posted 11 May 2013 - 03:17 AM

any help with that..i have no clue

### #11 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11449
• Posts: 43,176
• Joined: 27-December 08

## Re: Calculation of delay in time

Posted 11 May 2013 - 01:36 PM

Quote

any help with that..i have no clue

Really? There are 60 seconds in a minute. I think you can figure out how to convert 11.5 minutes to minutes and seconds.

### #12 psh

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

## Re: Calculation of delay in time

Posted 11 May 2013 - 02:04 PM

macosxnerd101, on 11 May 2013 - 08:36 PM, said:

Quote

any help with that..i have no clue />

Really? There are 60 seconds in a minute. I think you can figure out how to convert 11.5 minutes to minutes and seconds.

its not the same topic.one is about to find delay in time and the second is about to find the average in time..thank you