Results 1 to 9 of 9

Thread: Java: Compare Dates/Timestamps?

  1. #1
    Join Date
    Mar 2008
    Location
    Ireland
    Beans
    829
    Distro
    Ubuntu 16.04 Xenial Xerus

    Java: Compare Dates/Timestamps?

    How can I compare these in an if statement?

    I keep running into this error:

    Type mismatch: cannot convert from int to boolean
    This seems like a big brick wall I keep hitting one way or another.
    Projects - PhotoFlare Image Editor | Xwii | URT-2D | BHR
    Hardware - System 76 - Galago UltraPro

  2. #2
    Join Date
    Apr 2005
    Beans
    105

    Re: Java: Compare Dates/Timestamps?

    Quote Originally Posted by fallenshadow View Post
    How can I compare these in an if statement?

    I keep running into this error:



    This seems like a big brick wall I keep hitting one way or another.
    http://stackoverflow.com/questions/2...son-using-java

  3. #3
    Join Date
    Oct 2011
    Location
    Chicago, IL
    Beans
    419
    Distro
    Xubuntu 10.04 Lucid Lynx

    Re: Java: Compare Dates/Timestamps?

    Timestamp has a compareTo(Date) method that you can check out.

  4. #4
    Join Date
    Mar 2008
    Location
    Ireland
    Beans
    829
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Java: Compare Dates/Timestamps?

    Thanks mehaga!

    I can now compare dates... however I also want to do time comparison. I tried modifying the code from stackoverflow but it doesn't seem to work.

    Code:
    int hour = trackEvent.getTimestamp().getHour(); //this equals 22 when tested
    
    currDtCal.set(Calendar.HOUR_OF_DAY, hour);
    I don't have any errors but im getting the hours as 00 even though the hours should be 22.
    Projects - PhotoFlare Image Editor | Xwii | URT-2D | BHR
    Hardware - System 76 - Galago UltraPro

  5. #5
    Join Date
    Apr 2005
    Beans
    105

    Re: Java: Compare Dates/Timestamps?

    Quote Originally Posted by fallenshadow View Post
    Thanks mehaga!

    I can now compare dates... however I also want to do time comparison. I tried modifying the code from stackoverflow but it doesn't seem to work.

    Code:
    int hour = trackEvent.getTimestamp().getHour(); //this equals 22 when tested
    
    currDtCal.set(Calendar.HOUR_OF_DAY, hour);
    I don't have any errors but im getting the hours as 00 even though the hours should be 22.
    You are welcome

    There are 3-4 interesting answers to that SO question. One of them, IIRC, resets hours to zero, in order to make date to date comparison work. Maybe you did that? In any case, see all the answers there, and see which one suits your situation best. If none of them work, ask a question there and you'll probably get an answer within minutes...

  6. #6
    Join Date
    Nov 2009
    Beans
    1,081

    Re: Java: Compare Dates/Timestamps?

    Consider migrating to joda-time.

    And in the future, consider spending more time making your questions more detailed and posting more relevant code snippets.

  7. #7
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,165
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Java: Compare Dates/Timestamps?

    Quote Originally Posted by fallenshadow View Post
    Thanks mehaga!

    I can now compare dates... however I also want to do time comparison. I tried modifying the code from stackoverflow but it doesn't seem to work.

    Code:
    int hour = trackEvent.getTimestamp().getHour(); //this equals 22 when tested
    
    currDtCal.set(Calendar.HOUR_OF_DAY, hour);
    I don't have any errors but im getting the hours as 00 even though the hours should be 22.
    Be very careful about timezones. A Java Date() is timezone independent. To get hours of day from a Date requires a timezone, explicit or not.

  8. #8
    Join Date
    Mar 2008
    Location
    Ireland
    Beans
    829
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Java: Compare Dates/Timestamps?

    @Some Penguin

    I have heard of Joda time but im not currently interested in using it. As for my code I thought it was relevant to what I was asking. :/

    To solve my problem I just changed to use "GregorianCalendar" as it is much easier to compare dates including the time and you can do direct comparisons.
    Last edited by fallenshadow; May 1st, 2012 at 10:23 AM.
    Projects - PhotoFlare Image Editor | Xwii | URT-2D | BHR
    Hardware - System 76 - Galago UltraPro

  9. #9
    Join Date
    Jun 2007
    Location
    Malvern, UK
    Beans
    992
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Java: Compare Dates/Timestamps?

    two weeks ago i was in hell with date/time stuff in java.
    joda time lib saved my life tbh.

Tags for this Thread

Bookmarks

Posting Permissions

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