English 中文(简体)
与日期时间一起工作以检查哪个更大
原标题:working with datetime to check which one is bigger

我有两个日期时间值, 我不知道如何比较它们。 我知道如果在( ) 和( ) 之前和( ) 之后的方法只有日期值, 那么我对日期时间值是否有效, 但我对日期值一无所知。 我所做的一切都低于 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 指属于 Google 日历的事件对象。这里我只想检查事件 e 是否已经超过今天的日期和时间。如果是的话,我想删除该事件。

最佳回答

您可以使用 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 日期时间:

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 的“Javadoc”表示,

public boolean isDatePass(DateTime date) {
    return date.isAfter(null); // but it does not take in account time zone
}
问题回答
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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签