mrbela
mrbela

Reputation: 4647

Compare Java Datetime with current time

I have written a program with a gui that contains a org.eclipse.swt.widgets.Datetime:

new DateTime(shell, SWT.BORDER | SWT.TIME);

The user can change its value to pick a specific time. Then he clicks something like a "send" button.

When this button is clicked, I want to validate, if the time picked in the DateTime is more than half an hour in the future regarded to the current system time.

I get the system time with the help of the java.util.calender:

Calendar cal = Calendar.getInstance();
cal.get(Calendar.HOUR_OF_DAY);
cal.get(Calendar.MINUTE);
cal.get(Calendar.SECOND));

Thank you for your help!!

Kind regards

Upvotes: 0

Views: 710

Answers (3)

Phil Shotton
Phil Shotton

Reputation: 11

Simply check whether your newly created Calendar object has a time value more than 1,800,000 ahead of the current time (that's 30 minutes * 60 * 1000):

The following method will return true if the time has been set beyond your limit (in minutes):

public boolean isOutOfLimit(DateTime dateTime, long minutes) {
    GregorianCalendar now = new GregorianCalendar();
    Calendar chosen = new GregorianCalendar(
            now.get(Calendar.YEAR),
            now.get(Calendar.MONTH),
            now.get(Calendar.DAY_OF_MONTH),
            dateTime.getHours(),
            dateTime.getMinutes(),
            dateTime.getSeconds());
    long millisecs = minutes * 60L * 1000L;
    return ((chosen.getTimeInMillis() - now.getTimeInMillis()) > millisecs) ? true : false;
}

Upvotes: 0

Alex
Alex

Reputation: 4473

        DateTime dt = new DateTime(shell, SWT.BORDER | SWT.TIME);

    {
        // some method invoked to compare time difference
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, dt.getYear());
        cal.set(Calendar.MONTH, dt.getMonth());
        cal.set(Calendar.DAY_OF_MONTH, dt.getDay());
        cal.set(Calendar.HOUR, dt.getHours());
        cal.set(Calendar.MINUTE, dt.getMinutes());
        cal.set(Calendar.SECOND, dt.getSeconds());

        if (cal.getTimeInMillis() - System.currentTimeMillis() > 30 * 60 * 1000) {
            // the time picked in the DateTime is more than half an hour in
            // the future regarded to the current system time.
        }
    }

Upvotes: 1

Blip
Blip

Reputation: 3171

I would suggest instead of adding a button to send the time you could use a SelectionListener to verify whether the time that was selected by the user is more or less than 30 minutes from the present time. You could look at the following code:

DateTime timeSelection = new DateTime(shell, DateTime.TIME);
timeSelection.addSelectionListener(new SelectionAdapter (){
   widgetSelected(SelectionEvent e){
      DateTime dateTime = (DateTime)(e.getSource());
      Calendar cal = Calendar.getInstance(); //Create a new instance of Calendar.
      cal.clear(); //Clear all the default values of the calendar object.
      cal.set(dateTime.getYear(), dateTime.getMonth(), dateTime.getDay(), dateTime.getHours(), dateTime.getMinutes(), dateTime.getSeconds()); 
      //Setting all the required fields of the calendar object to the user's selected values. 
      cal.add(Calendar.Minute,-30) //Reduce the user time by 30 minutes so that if we compare the user's time with current time +30 minutes.
      if(cal.after(Calendar.getInstance())){
          //The selected time is more than 30 minutes after current time.
      }else{
          //The selected time is less than current time + 30 minutes
      }
   }
});

Upvotes: 1

Related Questions