当时间比较评估为真时,如何实时更新 Java 中的 Excel .csv 文件?

How to make real-time updates of an Excel .csv file in Java when time comparison evaluates as true?

首先我会说,如果有更简单的方法,我愿意接受其他建议。我的目标是每天在美国东部标准时间上午 9 点和下午 4 点(不是我的时区)用股票数据更新 CSV 文件。我觉得我很亲近。我现在拥有的包括一个 getEST 函数,该函数提取 EST 时区中的当前时间(正确考虑夏令时)和 returns 布尔值 true/false 如果该时间与我硬连线的开始或结束时间匹配.我也有程序创建一个 CSV 文件并每次都附加到它。我需要对我的代码进行一些修改,以便在这些布尔标志输出为真时自动执行我的数据检索代码。

我现在已将其设置为利用重置计数器告诉它等待 7 或 17 小时,具体取决于该计数器的值。所以我的问题是 a) 当 EST 时钟到达午夜时推动重置会更好吗?和 b) 我如何随时启动程序,以便它在正确的时间输出(即,如果应用程序在美国东部时间 6 点启动,它会等待 3 小时才能提取数据?

由于我有很多代码,所以我会截取一些代码。让我知道是否有任何遗漏部分会有所帮助。我知道我的封装也可以改进,只是想让它先工作,然后再将更多变量更改为私有。

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");

我能够通过构建一个新函数来解决这个问题,该函数使用现代 Java.time 实用程序计算当前时间(美国东部标准时间)和下一个市场 open/close 时间之间的持续时间:

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;
}
}

它接受负值(即自开市以来的时间),如果已过收盘时间,则将其转换为正时间。通过从 24 中减去,它 returns 正确的时间值,直到市场再次开盘并在适当的时间成功启动