Results 1 to 6 of 6
Thread: Addition of Two time
- 12-27-2008, 01:14 PM #1
Member
- Join Date
- Jun 2008
- Posts
- 39
- Rep Power
- 0
-
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.
-
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.
- 12-27-2008, 02:02 PM #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();
}
}
}
- 12-27-2008, 05:02 PM #5
huh?
So what's the problem you're having?
An observation:
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.Java Code:[B][COLOR="red"]hours[/COLOR][/B] = 1000*60*60*hours; [B][COLOR="red"]minutes[/COLOR][/B] = 1000*60*minutes;
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-29-2008, 11:09 AM #6
Member
- Join Date
- Jun 2008
- Posts
- 39
- Rep Power
- 0
Similar Threads
-
Addition java program
By tabrez_k81 in forum New To JavaReplies: 5Last Post: 12-15-2008, 10:08 AM -
Addition Problem
By tornado in forum New To JavaReplies: 6Last Post: 12-06-2008, 11:37 PM -
string addition??
By j2vdk in forum New To JavaReplies: 2Last Post: 09-03-2008, 03:41 PM -
Binary Addition
By Deo Favente in forum Advanced JavaReplies: 11Last Post: 04-24-2008, 05:34 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks