0

How to make condition like

the code will only run between

9AM to 10PM How do I make it on if statement? or whatever method you guys have?

if(timeNow >= 9PM && timeNow <= 10PM){
//do coding
}else{
//Code won't execute because its not inside of the scope.
}
John Smith
  • 21
  • 5
  • 2
    More context, please. Can you post the code you want to run? Feels more like a scheduler or scripting problem, not Java. – duffymo Mar 01 '19 at 15:11
  • Can you modify the function/class? Is AspectJ an option? – lalo Mar 01 '19 at 15:12
  • I don't know what the proper code for this but I want to make my code only run for between 9AM to 10PM. – John Smith Mar 01 '19 at 15:15
  • I really don't have idea guys how to do this that is why I am asking for help – John Smith Mar 01 '19 at 15:15
  • @JohnSmith Could you share more details of the requirement? If not code at least an example of what the code does. – lalo Mar 01 '19 at 15:16
  • I edited my post, thats the code I want to make. – John Smith Mar 01 '19 at 15:18
  • What kind of framework do you use? Do you use any extern library? – curiouscupcake Mar 01 '19 at 15:20
  • no I only use java, for my javafx project – John Smith Mar 01 '19 at 15:22
  • 1
    Similar problem https://stackoverflow.com/a/37632445/2804966 – lalo Mar 01 '19 at 15:26
  • So you want your app to only be able to start between 9am and 10pm? What if it's already running when 10pm hits? Should it get the time from the local device ( then changing the device time would allow you to bypass this limit ) or should it listen to time from an online api ( then you would require an internet connection to be able to start the app ). – Ivan Furdek Mar 01 '19 at 15:27

1 Answers1

0

If you don't mind time zone, you can use LocalDateTime. getHour() method will return hour-of-day (0 to 23).

int timeNow = LocalDateTime.now().getHour();
if(timeNow >= 9 && timeNow <= 10) {
  ...
} else {
  ...
}
ntalbs
  • 28,700
  • 8
  • 66
  • 83