java time check API
Posted
by ring bearer
on Stack Overflow
See other posts from Stack Overflow
or by ring bearer
Published on 2010-03-18T20:09:10Z
Indexed on
2010/03/18
20:11 UTC
Read the original article
Hit count: 397
I am sure this was done 1000 times in 1000 different places. The question is I want to know if there is a better/standard/faster way to check if current "time" is between two time values given in hh:mm:ss
format. For example, my big business logic should not run between 18:00:00 and 18:30:00
. So here is what I had in mind:
public static boolean isCurrentTimeBetween(String starthhmmss, String endhhmmss) throws ParseException{
DateFormat hhmmssFormat = new SimpleDateFormat("yyyyMMddhh:mm:ss");
Date now = new Date();
String yyyMMdd = hhmmssFormat.format(now).substring(0, 8);
return(hhmmssFormat.parse(yyyMMdd+starthhmmss).before(now) &&
hhmmssFormat.parse(yyyMMdd+endhhmmss).after(now));
}
Example test case:
String doNotRunBetween="18:00:00,18:30:00";//read from props file
String[] hhmmss = downTime.split(",");
if(isCurrentTimeBetween(hhmmss[0], hhmmss[1])){
System.out.println("NOT OK TO RUN");
}else{
System.out.println("OK TO RUN");
}
What I am looking for is code that is better
- in performance
- in looks
- in correctness
What I am not looking for
- third-party libraries
- Exception handling debate
- variable naming conventions
- method modifier issues
© Stack Overflow or respective owner