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 jav开发者_如何学Ca.util.date
. I take solution with calendar
as well
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).
java.time
The question uses the java.util
date-time API which was the right thing to do using the Java standard library in 2011. In March 2014, the modern Date-Time API was released as part of the Java 8 standard library which supplanted the legacy date-time API and since then it is strongly recommended to switch to java.time
, the modern date-time API.
Also, notice the following message on the Home-Page of Joda-Time:
Note that from Java SE 8 onwards, users are asked to migrate to
java.time
(JSR-310) - a core part of the JDK which replaces this project.
Solution using java.time
You can use ZonedDateTime
to specify date-time along with its ZoneId
.
Demo:
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
class Main {
public static void main(String[] args) {
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("America/New_York"));
ZonedDateTime then = now.withHour(14).truncatedTo(ChronoUnit.HOURS);
System.out.println(now);
System.out.println(then);
// Compare
if (now.isBefore(then))
System.out.println(now + " is before " + then);
else if (now.isEqual(then))
System.out.println(now + " is equal to " + then);
else
System.out.println(now + " is after " + then);
// Another ways to compare
if (!now.isAfter(then))
System.out.println(now + " is either before or at " + then);
}
}
Output as of now:
2023-01-31T11:48:40.191880-05:00[America/New_York]
2023-01-31T14:00-05:00[America/New_York]
2023-01-31T11:48:40.191880-05:00[America/New_York] is before 2023-01-31T14:00-05:00[America/New_York]
2023-01-31T11:48:40.191880-05:00[America/New_York] is either before or at 2023-01-31T14:00-05:00[America/New_York]
ONLINE DEMO
Learn more about the modern Date-Time API from Trail: Date Time.
Do not use three-letter timezone ID: Note from the Java 7 Timezone
documentation:
Three-letter time zone IDs
For compatibility with JDK 1.1.x, some other three-letter time zone IDs (such as "PST", "CTT", "AST") are also supported. However, their use is deprecated because the same abbreviation is often used for multiple time zones (for example, "CST" could be U.S. "Central Standard Time" and "China Standard Time"), and the Java platform can then only recognize one of them.
You should use the date object instead of using a string. To compare use the method date.after and date.before.
精彩评论