比较当前的EST到美国东部时间下午2点?怎么样?

时间:2011-05-11 18:27:15

标签: java time timezone

所以我有这段代码可以返回当前的EST

Date easternTime = new Date();
DateFormat format = new SimpleDateFormat("h:mm a");
format.setTimeZone(TimeZone.getTimeZone("EST"));
return format.format(easternTime);

让它返回x = 12:15PM

我想比较x到美国东部时间下午3:00,看看美国东部标准时间下午3点之前或之后的x,和/或是美国东部时间下午3点到6点之间的x。有没有办法做到这一点。

我们不必使用java.util.date。我也使用calendar解决方案

2 个答案:

答案 0 :(得分:3)

我会肯定Joda Time执行此操作。如果确实希望使用内置API执行此操作,则需要使用Calendar来查找特定时区的本地时间 - 但Joda会使其更加简单。< / p>

您使用的是DateTime,该{{3}}位于特定时区,可能会从中LocalTime与您硬编码的某些LocalTime进行比较(或从配置文件等中读取。)

答案 1 :(得分:1)

您应该使用日期对象而不是使用字符串。要比较使用方法date.after和date.before。