I'll start by saying I'm open to other suggestions if there's an easier way to do this. My goal is to update a CSV file with stock data at 9 am and 4 pm EST (not my time zone) daily. I feel I am very close. What I have now includes a getEST function that pulls the current time in the EST timezone (properly accounting for Daylight Savings) and returns a boolean value of true/false if that time matches the opening or closing time I hardwired. I also have the program creating a CSV file and appending to it each time. I need to make some modifications to my code so that it automatically executes my data retrieval code when these boolean flags output as true.
I have it set up right now to utilize a reset counter telling it to wait either 7 or 17 hours depending on the value of that counter. So my question is a) Would it be better to push the reset when the EST clock reaches midnight? and b) How do I start the program at any time so that it outputs at the right time (i.e. if the app launches at 6 EST it waits 3 hours before pulling the data?
I'll snippet the code a bit since I have a lot. Let me know if any missing portion would be helpful. I'm aware my encapsulation can be improved as well, just want to get it working first and then change more variables to private after.
public class Scheduler {
//Declare common variables
static int reset = 0;
public static void main(String[] args) {
//Manual implementation for testing purposes
DJIA obj = new DJIA();
EST tu = new EST();
obj.getDJIA();
tu.getEST();
try {
obj.createCSV();
} catch (IOException e) {
e.printStackTrace();
}
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
Runnable runnable = new Runnable() {
public void run()
{
//Moving implementation code here causes program to wait 7 hours before executing
}
};
// Execute program at 9 am EST and 4 pm EST
if(reset == 0) {
service.schedule(runnable, 7, TimeUnit.HOURS);
reset ++;
}
else {
service.schedule(runnable, 17, TimeUnit.HOURS);
reset = 0;
}
}
}
public class EST {
//Make time strings usable by main class
public String est;
public String o_time;
public String c_time;
public boolean marketOpen;
public boolean marketClose;
//Gets timezone in EST
public void getEST() {
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("EST5EDT"));
est = dateFormat.format(new Date());
checkTime();
//System.out.println("Current time (EST): " + est);
//System.out.println("Is it 9 am EST? " + marketOpen);
}
//Check if the stock market is open or not, returning a boolean indicator
public void checkTime() {
o_time = "09:00:00";
c_time = "16:00:00";
if(est.equals(o_time)) {
marketOpen = true;
}
else {
marketOpen = false;
}
if(est.equals(c_time)) {
marketClose = true;
}
else {
marketClose = false;
}
}
}
//Snipet from DJIA class
public void makeCSV() throws IOException {
//Create output file only if it does not already exist
File f = new File("DJIA.csv");
path = Paths.get("C:\\Users\\zrr81\\eclipse-workspace\\SelTest\\DJIA.csv");
if(!f.exists()) {
try {
//Create FileWriter object with file as parameter
String fid = "DJIA.csv";
CSVWriter writer = new CSVWriter(new FileWriter(fid, true));
String[] headers = "Stock, Opening Price, Closing Price".split(",");
writer.writeNext(headers);
writer.close();
}catch (Exception ex) {
ex.printStackTrace();
}
}
else {
}
}
public void createCSV() throws IOException {
//Insert if else logic to import to proper array
makeCSV();
o_values.add(stock);
o_values.add("test");
c_values.add("#Empty");
c_values.add("test");
I was able to solve this by building a new function that calculates the time duration between the current time (in EST) and the next market open/close time using the modern Java.time utility:
public class TimeUntil extends EST {
//Create commonly used EST function
EST est = new EST();
public static LocalTime d1;
public static LocalTime d2;
public static LocalTime d3;
public static long diff;
public int wait = 0;
//Main time calculator
public void calcTime() {
est.getEST();
try {
d1 = LocalTime.parse(est.est);
d2 = LocalTime.parse(est.o_time);
d3 = LocalTime.parse(est.c_time);
Duration duration = Duration.between(d1, d2);
diff = duration.toHours();
if(diff > 0 || diff < -7) {
tillOpen();
}
else {
tillClose();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public int tillOpen(){
if(diff > 0) {
wait = (int) diff;
return wait;
}
else {
int x = Math.abs((int) diff);
wait = 24 - x;
return wait;
}
}
public int tillClose() {
wait = Math.abs((int) diff);
return wait;
}
}
This accepts negative values (i.e. how long it's been since market open) and translates them into a positive time if it's past market close. By subtracting from 24, it returns the correct value for time until the market opens again and successfully launches at the proper time