1. Member
Join Date
Jun 2008
Posts
39
Rep Power
0

## Addition of Two time

Hello friends,

I need addition of given times. please help me. i have given input below.......

00:02 and 9:0 it is in hours and minutes......... pls give a solution.

2. If this is simple addition as you noted above, then create a method to convert your time to minutes or seconds (depending on how fine-grained you need this), add your numbers, then use another method to convert back to hours, minutes, etc...

If on the other hand you're talking about adding times and dates, then you'll need to use a SimpleDateFormat object to convert a String to a Date object, add milliseconds, then convert back again with the SimpleDateFormat.

In short, your solution will depend on the precise problem at hand, a problem that has not been fully specified in your original post.

3. Also, please do not cross-post questions in multiple forums. This will frustrate anyone who tries to help you only to find out later that the same answer was given hours ago in a cross-posted thread. For this reason, many volunteers here and at the other sites refuse to help repeat offenders.

4. Member
Join Date
Jun 2008
Posts
39
Rep Power
0
Than q for ur valuable reply . i have done it .

here is the code........

import java.util.*;
import java.text.*;

class Timesubtraction
{
public static void main(String ar[])
{
try{
String date_time = "2008-08-04 00:02:05";
String stop_time = "9:0";

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

java.util.Date date = sdf.parse(date_time);

long hours = Long.parseLong(stop_time.split(":")[0]);
long minutes = Long.parseLong(stop_time.split(":")[1]);
hours = 1000*60*60*hours;
minutes = 1000*60*minutes;
long time = (date.getTime() + (hours+minutes));
date.setTime(time);
System.out.println("The result is = " + sdf.format(date));
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

5. ## huh?

So what's the problem you're having?
An observation:
Java Code:
```[B][COLOR="red"]hours[/COLOR][/B] = 1000*60*60*hours;
[B][COLOR="red"]minutes[/COLOR][/B] = 1000*60*minutes;```
Please define your variables better. For example, don't reuse the hours and minutes variables when converting to milliseconds... use other variables like milliHours and milliMinutes. It helps understand the code.

6. Member
Join Date
Jun 2008
Posts
39
Rep Power
0
Than q for ur comment i will correct.......

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•