Results 1 to 3 of 3

Thread: Date math

  1. #1
    orchid's Avatar
    orchid is offline Member
    Join Date
    Apr 2007
    Rep Power

    Question Date math

    Am new to java and having an issue trying to do some date math.
    I made this test code and it returns a date difference of 24 days which is what I wanted. My question is this the correct way to subtract dates or should I be doing something more sophisticated?
    Java Code:
    private void playWithDates()
        Calendar aDay = new GregorianCalendar();
        aDay.set(2007, Calendar.JANUARY, 25, 0, 0, 0);
        Calendar newYearsDay = new GregorianCalendar();
        newYearsDay.set(2007, Calendar.JANUARY, 1, 0, 0, 0);
        long nyd=newYearsDay.getTimeInMillis();
        long a=aDay.getTimeInMillis();
        long diff =a-nyd;
    output is: 24

  2. #2
    derrickD is offline Member
    Join Date
    Apr 2007
    Rep Power


    Well that does work so I'd say if it meets your needs then go for it.
    There is a very large and involved discussion on date math that you might want to read here:
    java date math

  3. #3
    goldhouse is offline Senior Member
    Join Date
    Mar 2007
    Rep Power


    Calendar class helps you to achieve this with the help roll method,
    The description is as below from javadoc of sun
    Java Code:
    public void roll(int field,
                     int amount)
        Add to field a signed amount without changing larger fields. A negative roll amount means to subtract from field without changing larger fields.
        Example: Consider a GregorianCalendar originally set to August 31, 1999. Calling roll(Calendar.MONTH, 8) sets the calendar to April 30, 1999. Using a GregorianCalendar, the DAY_OF_MONTH field cannot be 31 in the month April. DAY_OF_MONTH is set to the closest possible value, 30. The YEAR field maintains the value of 1999 because it is a larger field than MONTH.
        Example: Consider a GregorianCalendar originally set to Sunday June 6, 1999. Calling roll(Calendar.WEEK_OF_MONTH, -1) sets the calendar to Tuesday June 1, 1999, whereas calling add(Calendar.WEEK_OF_MONTH, -1) sets the calendar to Sunday May 30, 1999. This is because the roll rule imposes an additional constraint: The MONTH must not change when the WEEK_OF_MONTH is rolled. Taken together with add rule 1, the resultant date must be between Tuesday June 1 and Saturday June 5. According to add rule 2, the DAY_OF_WEEK, an invariant when changing the WEEK_OF_MONTH, is set to Tuesday, the closest possible value to Sunday (where Sunday is the first day of the week).
            roll in class Calendar
            field - the time field.
            amount - the signed amount to add to field.
        See Also:
            add(int, int), Calendar.set(int, int)

Similar Threads

  1. Math Class
    By ritwik07 in forum New To Java
    Replies: 2
    Last Post: 09-14-2009, 04:06 PM
  2. Java Math
    By levent in forum Java Tutorial
    Replies: 1
    Last Post: 05-12-2008, 09:03 AM
  3. 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
  4. Math.Random
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-23-2007, 03:09 PM
  5. Help with math in java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 06:05 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