Compare time from given date with two given time strings using Java -
i have method takes argument of 2 time strings eg: t1 - 09:00 , t2 - 15:00 , date argument.
i want achieve solution given time date or within passed timeframe or not.
thanks in advance
public class checktime { private boolean isopen; public boolean isvalid(string time1, string time2, date date) { ----logic goes here------ return isopen; } public static void main(string[] args) { new checktime().isvalid("09:00","15:00", new date(system.currenttimemillis())); } }
the first thing values format can compared. mind, that's using java 8's time api (or jodatime or such api if you're still using earlier version of java)
date = new date(); localdatetime ldt = localdatetime.ofinstant(now.toinstant(), zoneid.systemdefault()); string startvalue = "09:00"; localtime starttime = localtime.parse(startvalue, datetimeformatter.iso_local_time);
from this, becomes simple matter of comparing values
system.out.println(ldt.tolocaltime().equals(starttime) || ldt.tolocaltime().isafter(starttime));
i'll leave figure out "before" comparison ;)
Comments
Post a Comment