Results 1 to 5 of 5
  1. #1
    Dominic is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default Comparing dates in java

    Hi,

    I'm trying to compare current date - date1 (which I would like to get automatically from Calendar.getInstance();) to some future date, so that when the dates are equal I would recieve a message like - The moon is full.

    I've gotten the following code, but I don't know how to make it work because I'm new to java, so if anybody could help I would be very gratefull?

    package com.mkyong.date;

    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;

    public class DateFuture
    {
    public static void main( String[] args )
    {
    try{

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date1 = sdf.parse("2010-01-31");
    Date date2 = sdf.parse("2010-01-31");

    System.out.println(sdf.format(date1));
    System.out.println(sdf.format(date2));

    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.setTime(date1);
    cal2.setTime(date2);


    if(cal1.equals(cal2)){
    System.out.println("The Moon is full");
    }

    }catch(ParseException ex){
    ex.printStackTrace();
    }
    }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default

    You have two basic options. The first is to set up an infinite loop, constantly checking the time, then printing whatever you want and exiting the loop when the time reaches the other time.

    The better way is to use a Timer or Thread, wait for the amount of time difference, and then print out whatever you want.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Dominic is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    I would like to implement this later into an android application so that when an activity starts, it would checked the current date, than compared it with another date and if equal displayed toast message. Do I still need a loop for that even thought it's a time thing?

  4. #4
    junnydc is offline Member
    Join Date
    Nov 2011
    Location
    Singapore
    Posts
    4
    Rep Power
    0

    Default Re: Comparing dates in java

    if somebody still want this. you could use this

    Date modDate = removeTime(your first date);
    Date curDate = removeTime(your 2nd date);

    if (modDate.before(curDate)) {
    ........
    }



    private Date removeTime(Date pDate){
    Calendar cal = Calendar.getInstance();
    cal.setTime(pDate);

    // Set time fields to zero
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);

    // Put it back in the Date object
    return cal.getTime();
    }

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: Comparing dates in java

    Quote Originally Posted by junnydc View Post
    if somebody still want this. you could use this
    Spoonfeeding is not helping. Besides, that doesn't actually answer the OP's actual question!
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. using dates in java
    By javapink in forum New To Java
    Replies: 4
    Last Post: 02-10-2011, 07:35 AM
  2. Comparing Arrays in Java
    By Daykeem in forum New To Java
    Replies: 6
    Last Post: 01-15-2011, 04:25 AM
  3. comparing dates
    By palls in forum New To Java
    Replies: 3
    Last Post: 08-13-2010, 01:08 PM
  4. UTC Dates
    By PedroCosta in forum Advanced Java
    Replies: 3
    Last Post: 04-01-2010, 06:08 PM
  5. Comparing dates
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-28-2008, 09:02 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
  •