0

I'm working on Android using Java and I have a question.

I want a button to be enabled only during a certain time period. Let's say I want the button to only work between 19:00 and 19:30 and after that it shows a error message, how would I achieve that?

I tried using something like this code, but it didn't work out:

public class LocalTimeDemo {
   public static void main(String[] args) {

      LocalTime time = LocalTime.now();
      LocalTime time1 = LocalTime.parse("12:35:30");
      System.out.println(time1.isBefore(time));  
   }
}
2
  • 1
    You can use AlarmManager and set alarm for specific time!. Nov 4, 2019 at 4:46
  • This link help you to visible button through thread start when your time1 is matched current time. Please see the link and try. stackoverflow.com/a/3392175 Nov 4, 2019 at 5:05

1 Answer 1

0

You have to use the calendar class for this. If you want to do it when activity is resumed then use this code snippet in the onResume method of activity

Calendar calendar = Calendar.getInstance();

    int tHour = calendar.get(Calendar.HOUR_OF_DAY);
    int tmin = calendar.get(Calendar.MINUTE);

    if (tHour = 19 && tmin > 0 && tmin<30){
        btn.setEnabled(true);
    }else{
        btn.setEnabled(false);
    }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.