Compare the current EST to 2PM EST? How?

你。 提交于 2020-01-16 20:03:31

问题


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 LocalTimes 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!