Results 1 to 4 of 4
  1. #1
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default Difference of Two Dates

    Hi,

    I am using the GregorianCalender class and I was just wondering how to get the difference of two dates. For example the difference between
    3/3/2009 and 1/1/2009 should be shown like this: 2/2/0

    Thank you!

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

  3. #3
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default

    I have tried this but I don't know how to put the 10/10/2010 in a way that can be subtracted, sdf.format(date.getTime()) has 12/12/2012.
    Java Code:
    long difference = sdf.format(date.getTime()) - 10/10/2010;

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

    Default

    GregorianCalendar and Calendar (on which the former is based) have methods that can help you here, especially the get(int field) method. If you look at the beginning of the GregorianCalendar API you'll even see sample code that shows how to use the get method. Have a look and give it a try.

    GregorianCalendar (Java Platform SE 6)

Similar Threads

  1. JTable and Dates
    By paluee in forum AWT / Swing
    Replies: 0
    Last Post: 03-30-2009, 09:02 PM
  2. Replies: 23
    Last Post: 02-19-2009, 06:24 AM
  3. Help with dates
    By bumblyb33 in forum New To Java
    Replies: 1
    Last Post: 02-13-2009, 03:54 PM
  4. How to Compare two Dates
    By Java Tip in forum java.util
    Replies: 1
    Last Post: 06-24-2008, 08:05 AM
  5. No fo days between two dates
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-28-2008, 10:06 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
  •