3

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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Shuwn Yuan Tee
  • 5,578
  • 6
  • 28
  • 42

1 Answers1

4

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);
Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
  • Very good answer by Meno Hochschild. For the suggestions, if you are working with times, rather than `new GregorianCalendar()` I would consider getting either [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) or Meno Hochschild(!)’s [Time4J](https://github.com/MenoData/Time4J). Everything else in the answer would still apply. – Ole V.V. Jun 08 '17 at 06:18
  • 1
    @OleV.V. I share your opinion that the old `Calendar`-API is not well designed according to current programming practices (people have judged differently in year 1995) but I did not mention alternative more modern libraries which have better style or more features because my guideline here on SO is to only suggest it if the concrete problem of OP cannot be solved by old design in a satisfactory way. I don't know how much the OP wants to invest in complex date/time-programming. About my lib Time4J, there is a sister project [Time4A](https://github.com/MenoData/Time4A) for the Android platform – Meno Hochschild Jun 08 '17 at 06:44
  • Thanks for your point. I also don’t see why `GregorianCalendar` won’t work OK on the short sight for determining whether current time is in the opening hours given. Do just a little bit more and one is likely to find it more cumbersome or errorprone than with the newer classes. I just read that JSR-310 seems to be coming to Android, so I fear that those who end up maintaining the code will curse the coder for having used the worn-out class. I may have turned into more of an evangelist than you on this point, I confess. – Ole V.V. Jun 08 '17 at 07:24
  • 1
    @OleV.V. Indeed, Android-O (level 25) will incorporate JSR-310 which makes Threeten-ABP unnecessary (while Time4A would still offer more features for those who need it). However, most mobile phones currently used by people will still have old Android OS versions for a long time. And I have even got the impression that Google does not deprecate the old awkward Calendar-API but even incorporate more and more from ICU4J (I don't like its API-style, too, it is similar style like `java.util.Calendar`), so future Android will offer at least 3 different date-time-APIs. – Meno Hochschild Jun 08 '17 at 07:34