-4

I have two datetime values and i dont knw how to compare them. I know if i had only date values then before() and after() methods would have worked but i have no idea about Datetime values. All i have done is below plz tell me if its correct ?? and plz do guide me if its not a good way and a better alternative is available.

Date now = new Date();
DateTime currenttime = new DateTime(now, TimeZone.getTimeZone("IST"));
DateTime edate = e.getEnd().getDateTime();                 
if(currenttime.getValue()>edate.getValue())
{
       //here I want to do the logic to delete this event.         
}

e refers to the event object that is of google calendar. All i want to do here is check if Event e is past todays date and time. and if it is then i wanna delete the event.

samir
  • 339
  • 6
  • 19
  • Have you tried it? Did it work as you expected? If not, what was the problem? – Mark Byers May 25 '12 at 06:06
  • @MarkByers i havent tried it. Because the code is not reaching to the point where it is written due to some errors. I just wanna knw if u think this is the right way ? – samir May 25 '12 at 06:08
  • can getValue() be used as i m using it?? – samir May 25 '12 at 06:08
  • What package is your DateTime from? Have you tried a "compareTo" method? Many classes implement the "Comparable" interface to allow easy comparison of values. – toniedzwiedz May 25 '12 at 06:10
  • there are very few methods available in these datetimes. its com.google.api.client.util package. there are methods namely notify(), equals(), isDateonly(), getClass(), getTimeZoneshift() like that . out of which i found only getvalues() sort of interesting – samir May 25 '12 at 06:16
  • have a look at this http://stackoverflow.com/questions/2517709/java-comparing-two-dates-to-see-if-they-are-in-the-same-day – Aamirkhan May 25 '12 at 09:29

2 Answers2

1

You can use jdk Calendar to get and check days:

public boolean isDatePass(Date date) {
    Calendar calendar = Calendar.getInstance();

    // Getting day of year and year of checked date:
    calendar.setTime(date);
    int checkedYear = calendar.get(Calendar.YEAR);
    int checkedDay = calendar.get(Calendar.DAY_OF_YEAR);

    // Getting day of year and year of current date:
    calendar.setTime(new Date());
    int currentYear = calendar.get(Calendar.YEAR);
    int currentDay = calendar.get(Calendar.DAY_OF_YEAR);

    if(checkedYear != currentYear) {
        return checkedYear < currentYear;
    }

    return checkedDay < currentDay;

}

For yoda DateTime:

public boolean isDatePass(DateTime date) {
    // Getting day of year and year of checked date:
    int checkedYear = date.getYear();
    int checkedDay = date.getDayOfYear();

    // Getting day of year and year of current date:
    DateTime currentTime = new DateTime(now, TimeZone.getTimeZone("IST"));
    int currentYear = currentTime.getYear();
    int currentDay = currentTime.getDayOfYear();

    if(checkedYear != currentYear) {
        return checkedYear < currentYear;
    }

    return checkedDay < currentDay;

}

Not days only but time:

public boolean isDatePass(DateTime date) {

    DateTime currentTime = new DateTime(now, TimeZone.getTimeZone("IST"));
    return date.isAfter(currentTime);
}

More simple solution (according to javadoc when pass null to isAfter/isBefore this mean current or now):

public boolean isDatePass(DateTime date) {
    return date.isAfter(null); // but it does not take in account time zone
}
alexey28
  • 5,170
  • 1
  • 20
  • 25
  • but the problem is i have datetime object with me. Now how to convert that into calendar?? i think if i can do that your way can help me – samir May 25 '12 at 07:04
  • Does yor DateTime have some Date getDate() or long getTimestamp() methods? If it does, you can create Date based on this. Just convert your DateTime to Date. – alexey28 May 25 '12 at 07:39
  • no it has methods like notify(), equals(), isDateonly(), getClass(), getTimeZoneshift() like that . out of which i found only getvalues() to be interesting as its doc says "returns date time value expressed as number of milliseconds". but i have a doubt that will it convert even date into milliseconds?? – samir May 25 '12 at 07:43
  • I add case for for Yoda DateTime parameters. – alexey28 May 25 '12 at 07:45
  • but buddy its only for day comparison while in my application i m working on google calendar so time is also of significance. – samir May 25 '12 at 07:47
  • If you nead to check time than it will be simpler. As I understood you need to check days. Use isBefore defined in DateTime. See third case in my answer. – alexey28 May 25 '12 at 07:58
  • thanks actually I wanted to check even time but date would have worked too. Plz check my answer. somehow this worked for me. – samir Jun 18 '12 at 06:05
0
public String deleteEvents() throws ParseException {
    try {

        boolean evtDelMsg = false;
        int iEvtCnt = 0;
        int totalEvents = lstEvents.size();

        System.out.println("events are :"+lstEvents.getItems().toString());

        if(lstEvents.size()>0)
        {
                for(Event e : lstEvents.getItems())
                {
                    System.out.println("startdate is "+e.getStart().toString());
                    Date now = new Date();                 

                   try
                   { 
                      if((new Date()).getTime() < e.getEnd().getDateTime().getValue())
                      {
                         evtDelMsg = EventManager.deleteEvent(getGoogleCalObj(), selectedCalId, e.getId());
                        iEvtCnt++; 
                      }
                   }
                   catch(NullPointerException npe)
                   {
                     System.out.println("edate is null so creating");
                     processImportedEventsData();
                   }
               }
        }
        else
        {
            System.out.println("no events in this calendar");
        }

        setInfoMsg("Successfully deleted " + iEvtCnt + " Events out of total " + totalEvents);

        createEventFlag = true;            
        processImportedEventsData();     

    } 
    catch (IOException ex)
    {
        Logger.getLogger(ManageCalendar.class.getName()).log(Level.SEVERE, null, ex);
    }       
    return null;
}

This one worked for me I simply used the long value of the event's i.e "e" date and time and compared with the todays date time.The getValue() method returns in long which is milliseconds. This made it a bit simple. And then in the loop i deleted all the events calling deleteEvent() of EventManager.

samir
  • 339
  • 6
  • 19