0

I'm looking for an efficient way to convert the hours and minutes i get from a TimePickerDialog into a Date object.

I need it for a method which has to know whether the input Date is at MAX 12 hours from now(). So maybe you can enligthen me into a way of doing this without even using Date objects.

PS : In my TimePickerDialog, I consider every Time there after now() (so for example : setting 5:00 at 8:00, is in 21 hours).

PS 2 : I use the 24h format.

Thank you :)

Mehdi
  • 713
  • 9
  • 19

1 Answers1

0

I don't think you're going to have to use the Date object for this. Here's a simple example. Let's say you have a button which on clicking presents you the TimePickerDialog.

The onClick() event of the Button would look something like this

@Override
public void onClick(View v) {

    // Process to get Current Time
    final Calendar c = Calendar.getInstance();
    mHour = c.get(Calendar.HOUR_OF_DAY);
    mMinute = c.get(Calendar.MINUTE);
    TimePickerDialog tpd = new TimePickerDialog(this,
            new TimePickerDialog.OnTimeSetListener() {

                @Override
                public void onTimeSet(TimePicker view, int hourOfDay,
                                      int minute) {
                    pHour = hourOfDay;
                    int difference = pHour-mHour;
                    if(difference < 0) {
                        int remaining = 24 + difference;
                        if(remaining <= 12) {
                            // Less than or equal to 12 hours from now
                            // isValidTime is a boolean variable defined at the top
                            isValidTime =  true;
                        }
                        else{
                            // More than 12 hours
                            isValidTime =   false;
                        }
                    }
                    else {
                        int remaining = difference;
                        if(remaining <= 12) {
                            // Less than or equal to 12 hours from now
                            isValidTime = true;
                        }
                        else{
                            // More than 12 hours
                            isValidTime = false;
                        }

                    }

                    // Call your method with the isValidTime parameter
                    yourMethodThatNeedsBoolean(isValidTime );

                }
            }, mHour, mMinute,false);

    tpd.show();

}

private void yourMethodThatNeedsBoolean(boolean valid) {
    if(valid) {
        Toast.makeText(MainActivity.this, "Time is valid", Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(MainActivity.this, "Time is invalid", Toast.LENGTH_LONG).show();
    }

}

Note that pHour, mHour and mMinute are integers and isValidTime is a boolean which I have already declared at the top of my code(not shown here).

Bidhan
  • 10,607
  • 3
  • 39
  • 50
  • If we used your code on the example he proposed : currentTime<8:00> , the time he want to set is <5:00> you code will produce 3 ? while what is needed for the function is to produce 21 , is this correct ? – A.Alqadomi May 22 '15 at 01:56
  • This follows the 24 hour clock system. – Bidhan May 22 '15 at 01:59
  • If it's 18:00, and I set it to 17:00, this will produce 1, which is incorrect, because it's in 23 hours. – Mehdi May 22 '15 at 08:33
  • I don't understand, it's supposed to be a boolean : it's _a method which has to know whether the input (hours,minutes) are at MAX 12 hours from now() or not_ – Mehdi May 22 '15 at 11:39
  • I have shown you the logic behind it. You should easily be able to extract a boolean value from it and call your method. Anyway I've edited my answer again. Tell me what else you want implemented. – Bidhan May 22 '15 at 12:17
  • Thank you :) ! (It's actually the other way around, input : hours/minutes, output : false/true) – Mehdi May 22 '15 at 12:36