使用datetime来检查哪一个更大

时间:2012-05-25 06:04:58

标签: java datetime date

我有两个日期时间值,我不知道如何比较它们。我知道如果我只有日期值,那么before()和after()方法会有效,但我不知道Datetime值。我所做的就是在plz下面告诉我它是否正确?如果它不是一个好方法并且有更好的选择,那么PLZ会引导我。

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指的是谷歌日历的事件对象。我想在这里做的就是检查事件e是否已经过了今天的日期和时间。如果是的话我想删除该事件。

2 个答案:

答案 0 :(得分:1)

您可以使用jdk日历获取和查看日期:

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;

}

对于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;

}

不仅仅是几天而是时间:

public boolean isDatePass(DateTime date) {

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

更简单的解决方案(根据javadoc传递null到isAfter / isBefore这个意味着当前或现在):

public boolean isDatePass(DateTime date) {
    return date.isAfter(null); // but it does not take in account time zone
}

答案 1 :(得分: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;
}

这个对我有用我只使用了事件的长值,即“e”日期和时间,并与今天的日期时间进行比较.getValue()方法以long为单位返回,即毫秒。这使它有点简单。 然后在循环中我删除了调用EventManager的deleteEvent()的所有事件。