Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2010
    Posts
    72
    Rep Power
    0

    Default date subtraction

    guys i am working on a project in which i have to subtract two dates to get the effort time.

    for eg.
    if i have an entry like "6/30/2009 06:27" and "6/30/2009 14:33"

    the output should come as 8 hrs 6 mins

    any suggestion will be highly appreciated

  2. #2
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    to get the user input you can use a scanner

    for this you need to
    Java Code:
    import java.io.*;
    import java.ui.*;
    you can make a scanner object as following...
    Java Code:
    Scanner myscannername = new Scanner(System.in);
    and than you can prompt the user to input something like this:
    Java Code:
    System.out.println("Enter day:");
    string1=myscannername.nextLine();
    System.out.println("Enter month");
    string2=myscannername2.nextLine();
    do that for year too.

    that was the first date you enter
    now do the same for the second date you want... and name them string4, string5, string6 or something.

    now for the date substraction
    you gonna need some if statements
    for example like this:
    Java Code:
    if (string3>string6) { //string3 and string 6 are the years
    int yearsdifference;
    yearsdifference=string3-string6;
    }
    Last edited by coltragon; 02-12-2010 at 07:22 AM. Reason: forgot a brace
    Beginner in Java Programming, Please don't trust my anwsers blind please :D

  3. #3
    Join Date
    Jan 2010
    Posts
    72
    Rep Power
    0

    Default

    i am fetching the date in mm-dd-yyyy hh:mm format from the database......now the mm-dd-yyyy is sane in both the case......i have to calculate the the difference between hh:mm of the two

    can "-" operator be use directly on the Date object or should i convert them in string or integer format

    :confused:

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    You can get the milliseconds between the two Dates by simply subtracting them, and then turn them into whatever format you want.

  5. #5
    Join Date
    Jan 2010
    Posts
    72
    Rep Power
    0

    Default

    thanx a lot

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    You want to use getTime() to get the time in millis.

  7. #7
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    lol, so my code was wrong :OP
    Beginner in Java Programming, Please don't trust my anwsers blind please :D

Similar Threads

  1. java.util.Date vs java.sql.Date
    By Jack in forum New To Java
    Replies: 5
    Last Post: 10-28-2010, 02:59 PM
  2. julian date to full date format
    By judy318 in forum New To Java
    Replies: 7
    Last Post: 11-02-2009, 12:17 PM
  3. Compare date input to database with current date
    By hungleon88 in forum Advanced Java
    Replies: 2
    Last Post: 11-25-2008, 08:10 AM
  4. Creating a Gregorian Calendar using a Date object gives date - 1
    By prachi_goliwadekar in forum New To Java
    Replies: 1
    Last Post: 05-08-2008, 08:32 PM
  5. Difference between current date and anothe date
    By vijay balusamy in forum New To Java
    Replies: 1
    Last Post: 04-16-2008, 04:15 PM

Posting Permissions

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