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).