How to check if currentTime is in between startTime and endTime. Unfortunately my domain class has to use startTime and endTime as Strings.
Update:
statTime is just time e.g. 6.00
endTime is just time eg 18:00
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Well the obvious approach is to parse both strings into whatever date/time representation you like (personally I prefer Joda Time) and then compare the current date/time with that. Have you tried that? Where have you become unstuck? If the problem is that they’re times rather than date/time values, Joda Time’s
LocalTimeclass is what you want. (It’s not clear why your domain representation needs to be strings, by the way – you should make your domain representation the most natural one, and convert it to/from text representations at boundaries where you absolutely have to, IMO.)I would strongly advise you not to use some sort of static call to obtain the current date and time. Instead, use dependency injection to inject a “current time service” as you would any other dependency – you can then unit test this with a “fake clock” which is controlled in the tests, and inject a real implementation (which could use
System.currentTimeMillisor whatever you want) for production.