2

i have a booking form that requires a user to input their details alongside a date. the user cannot submit a date that is within 24 hours. a booking must be made after 24 hours.

how can i implement this?. i have obtained the current date and time.

so if the current date and time is 19062012 1324 the booking cannot be made until 20062012 1324

what i tried to do is this:

long mdates = (long) (Long.parseLong(date.getText().toString()));
    long mprefered= (long) (Long.parseLong(date2.getText().toString()));
        long sub = mprefered - mdates;

    if (preferedDateEditText.getText().toString() != null
            && !preferedDateEditText.getText().toString()
                    .equalsIgnoreCase("") && sub>100000000) {
        emailBody += "Prefered date & Time:"
                + preferedDateEditText.getText().toString().trim()
                + "\n sub="+sub;
    } else {
        errorMessage += "A booking cannot be made within 24 hours.";
    }

this works however if the prefered date is 01072012 1324 then it wont accept as being 24 hours in advance any help would be appreciated

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
Tuffy G
  • 1,521
  • 8
  • 31
  • 42

4 Answers4

2

Date date1 = new Date(millis);

Date date2 = new Date(millis);

        if(date1.compareTo(date2)>0)
        {

        }
        else if(date1.compareTo(date2)<0)
        {

        }
        else 
        {
            //both are equal
        }
KK_07k11A0585
  • 2,381
  • 3
  • 26
  • 33
1
Date date = new Date(long);
if (date.before(other_date)) {
...
Gorets
  • 2,434
  • 5
  • 27
  • 45
1

Use SimpleDateFormat to parse your date strings into Date objects. Then you can use those objects for comparison.

ekholm
  • 2,543
  • 18
  • 18
  • See this question for more info http://stackoverflow.com/questions/3960661/android-difference-between-two-dates-in-seconds – pilotcam Jun 19 '12 at 12:41
0

You can use Date and DateFormat to pares your date strings to Date objects, and then use the Date.before(Date) and Date.after(Date) functions to compare.

public checkTimePassed(String datestring){
        DateFormat dfm = new SimpleDateFormat("ddMMyyyy hm");
        Date preferred_date = dfm.parse(datestring);
        Date now = new Date();
        double minimum_time = preferred_date.getTime() + (24 * 60 * 60);
        return now.getTime() > minimum_time;
    }
Jurgen
  • 343
  • 3
  • 13