Results 1 to 6 of 6
  1. #1
    jithan is offline Member
    Join Date
    Jun 2008
    Posts
    39
    Rep Power
    0

    Default 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.

    Thanks in advance.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #4
    jithan is offline Member
    Join Date
    Jun 2008
    Posts
    39
    Rep Power
    0

    Default

    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. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default 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.
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    jithan is offline Member
    Join Date
    Jun 2008
    Posts
    39
    Rep Power
    0

Similar Threads

  1. Addition java program
    By tabrez_k81 in forum New To Java
    Replies: 5
    Last Post: 12-15-2008, 10:08 AM
  2. Addition Problem
    By tornado in forum New To Java
    Replies: 6
    Last Post: 12-06-2008, 11:37 PM
  3. string addition??
    By j2vdk in forum New To Java
    Replies: 2
    Last Post: 09-03-2008, 03:41 PM
  4. Binary Addition
    By Deo Favente in forum Advanced Java
    Replies: 11
    Last Post: 04-24-2008, 05:34 AM

Posting Permissions

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