0

I will save two times from time picker. One is named start and one is named end maybe 1.0PM and 2.0PM respectively. I need to check if a user can take lunch or do a certain task in that time span or certain range of time on a differrent and any date.

I am not sure which function to use. This is what I thought of trying:

  1. Get start and end from time picker and set to a calender then get timeInMillis and save for both. Then get Current System.timeInMillis on a different date and on a calender negate till that day of start and end days saving and get timeInMillis from that calender for both and check if current time falls in their range.
  2. Use any example from doc. But I'm confused and cant get the right way.
Rifat
  • 1,700
  • 3
  • 20
  • 51

1 Answers1

0

There is already a function for this in the api from LocalDateTime

    LocalDateTime startTime = LocalDateTime.of(2022, 01, 01, 14, 0);
    LocalDateTime endTime = LocalDateTime.of(2022, 01, 01, 15, 0);
    
    LocalDateTime someTime = LocalDateTime.of(2022, 01, 01, 14, 30); //LocalDatetime.now(); in your use case
    
    if(someTime.isAfter(startTime) && someTime.isBefore(endTime)) {
        System.out.println("User can take a break");
    } else {
        System.out.println("user can not take a break");
    }

If you are still working with java.util.Date the don´t ;). If there is no way around using Date the simply convert the Date to LocalDateTime and using its api Converting between java.time.LocalDateTime and java.util.Date. LocalDateTime is one of java8 most comfortable features

GJohannes
  • 1,408
  • 1
  • 8
  • 14
  • Right about that. But this should be simpler. Becuase I want this without Date or with Hours and Minutes only. I'll follow my approach of using a Calender instance. – Rifat Apr 16 '22 at 13:22