Results 1 to 5 of 5

Thread: Subtracting Dates [Resolved]

  1. #1

    Thread Starter
    Addicted Member MethadoneBoy's Avatar
    Join Date
    Oct 2001
    Location
    Preferably somewhere between Keira Knightley and Diane Kruger but I'm not fussy
    Posts
    180

    Subtracting Dates [Resolved]

    Hi everyone,

    I'm trying to write a program that regarding dates. Specifically, what I'm trying to do is this : compare two dates to see if one is less than 10 days away from the other.

    EG

    Code:
    int days = 10;
    
    if ( thisDate - thenextdate > days ) System.out.println ( "Less that 10 days to go" );
    else System.out.println ( "No problems" );
    Any help would be appreciated

    Thanks
    Last edited by MethadoneBoy; Jan 15th, 2005 at 04:02 PM. Reason: Reolved Problem
    "'Oh, hello Mr. Crick! What do you think of Jeffrey Archer?' Clip-clip-clip! Oh, come on! Who are you kidding? You wait til I'm mayor, you'll see how tough I am! Christ almighty...."

  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Subtracting Dates

    Well, what kind of problems is it generating?

    Are you using a Calendar object to get the date or a Data object?

    You could create a gregorian calendar object and then calculate the elapsed time.

    Code:
    import java.util.*;
    public class ElapsedMillis {
       public static void main(String[] args) {
          GregorianCalendar date1 = new GregorianCalendar(1969, Calendar.JULY, 16, 9, 32);
          GregorianCalendar gc2 = new GregorianCalendar(1995, 11, 1, 3, 2, 2);
          // the above two dates are one second apart
          Date d1 = date1.getTime();
          Date d2 = gc2.getTime();
          long l1 = d1.getTime();
          long l2 = d2.getTime();
          long difference = l2 - l1;
          System.out.println("Elapsed milliseconds: " + difference);
       }
    }

    I'm not for sure that this will work or that is what you want. I can't test it right now because I'm not at my laptop with the jdk.

  3. #3
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Subtracting Dates

    Also, I think this checks the difference in milliseconds, so you might have to do some converting or something. But maybe someone else will help you that knows more than me.

  4. #4

    Thread Starter
    Addicted Member MethadoneBoy's Avatar
    Join Date
    Oct 2001
    Location
    Preferably somewhere between Keira Knightley and Diane Kruger but I'm not fussy
    Posts
    180

    Re: Subtracting Dates

    No, actually what you have there is exactly what I was looking for.

    Thanks!
    "'Oh, hello Mr. Crick! What do you think of Jeffrey Archer?' Clip-clip-clip! Oh, come on! Who are you kidding? You wait til I'm mayor, you'll see how tough I am! Christ almighty...."

  5. #5
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Subtracting Dates [Resolved]

    Ahh cool. Glad I could help.

Posting Permissions

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



Click Here to Expand Forum to Full Width