javaandroidtimezonestocksjava.util.calendar

Java Calendar TimeZone for US Stock Market


I am doing Android programming related to US NYSE & NASDAQ stock market. So far I know that they are using Eastern Time (ET). Stock market opens at 9.30AM ET, closes at 4pm ET.

In order to check stock market is currently open / close, I want to check whether current time is within 9.30am - 4pm.

Is the below code correct, to get user time in ET timezone?

Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("US/Eastern"));

As I read, Eastern Time (ET) observe Daylight saving. In summer it uses EDT which is UTC-4, whereas in winter it uses EST which is UTC-5. So NYSE & NASDAQ is using ET timezone?

I want to make sure my comparison of 9.30am - 4pm is using same timezone as NYSE & NASDAQ stock market.


Solution

  • Instead of the outdated id "US/Eastern", I would choose "America/New_York" according to the tzdb maintained by IANA.

    This timezone takes into account daylight saving during summer (but not winter) and can hence be applied throughout the whole year.

    Wikipedia indicates that NYSE & NASDAQ stock market uses this timezone, too.

    By the way, I would use new GregorianCalendar() instead of Calendar.getInstance() to make sure that you get the right calendar (relevant if you are in Thailand).

    Evaluating the open-condition might look like this (half-open interval for local time "9:30/16:00"):

        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        boolean open = (hour > 9 || (hour == 9 && minute >= 30)) && (hour < 16);