问题
So I have this piece of code that would return the current EST
Date easternTime = new Date();
DateFormat format = new SimpleDateFormat("h:mm a");
format.setTimeZone(TimeZone.getTimeZone("EST"));
return format.format(easternTime);
let say it return x = 12:15PM
I want to compare x
to 3:00PM EST to see if x before or after 3:00PM EST, and/or is x between 3:00PM - 6:00PM EST. Is there a way to do this.
We dont have to use java.util.date
. I take solution with calendar
as well
回答1:
I would definitely do this in Joda Time instead. If you really want to do this with the built-in API, you need to use Calendar
to find the local time in a particular time zone - but Joda would make it much simpler.
You'd use a DateTime which is in a specific time zone, and possibly take the LocalTime
from that to compare with some LocalTime
s you've hard-coded (or read from a configuration file etc).
回答2:
You should use the date object instead of using a string. To compare use the method date.after and date.before.
来源:https://stackoverflow.com/questions/5968842/compare-the-current-est-to-2pm-est-how