gpt4 book ai didi

java - 在if语句中解析日期并仅显示规定时间范围内的结果

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:40:26 24 4
gpt4 key购买 nike

目前我正在编写一个 java 程序来找出在哪一天 Hammer或其他Candlestick pattern形成。

用户在执行程序时必须输入 2 个日期作为参数,例如java ReadingTest 2016-09-03 2016-10-31,程序会在 2016-09-03 到 2016-10-31 期间寻找 Hammer 模式。

代码如下:

import java.io.*;
import java.util.*;
import java.text.*;

public class ReadingTest
{
public static void main(String[] args) throws IOException,ParseException
{
//Import file to java
File file = new file("table.csv");

//Read the file
Scanner infile = new Scanner(file);

//Skip the first line in table.csv
infile.nextLine();

//Define format of date
SImpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

//Name the variables user enters
Date start = sdf.parse(args[0]);
Date end = sdf.parse(args[1]);

//Create ArrayList for each column of data
ArrayList<String> date = new ArrayList<String>();
ArrayList<Double> open = new ArrayList<Double>();
ArrayList<Double> high = new ArrayList<Double>();
ArrayList<Double> low = new ArrayList<Double>();
ArrayList<Double> close = new ArrayList<Double>();

while (infile.hasNext())
{
//Tokenize columns by comma
String[] data = infile.nextLine().split(",");
//Organize each column of data to one index of data array
date.add(data[0]);
open.add(Double.parseDouble(data[1]));
high.add(Double.parseDouble(data[2]));
low.add(Double.parseDouble(data[3]));
close.add(Double.parseDouble(data[4]));
}
//Show options and ask user to choose
System.out.println("1. Hammer");
System.out.println("2. Three white soldiers");
System.out.println("3. Bullish kicker");

//Record user input and execute corresponding code
Scanner input = new Scanner(System.in);
int choice = input.nextInt();

if (choice == 1)
for (int i = 0; i < date.size(); i++)
if (close.get(i) > open.get(i) &&
close.get(i) > ((high.get(i)) + (low.get(i)))/2 &&
((close.get(i) - low.get(i))/2 > (high.get(i) - close.get(i)))
System.out.println("Pattern found: " + date.get(i));
}
}

代码到这里为止都可以正常工作。但是,最后一行代码的输出是 dd/MM/yyyy 格式,我尝试使用 sdf.parse(date.get(i)) 而不是 date.get(i) 以 yyyy-MM 显示结果-dd 格式。使用 sdf.parse(date.get(i)) 运行代码会返回以下错误:

Exception in thread "main" java.text.ParseException: Unparseable date: 
"25/10/2016" at
java.text.DateFormat.parse(Unknown source) at ReadingTest.main(ReadingTest.java:59)

我还尝试只显示显示 Hammer 的日期使用:

(date.get(i).after(start) && date.get(i).before(end))

结果是

error: cannot find symbol
symbol: method before(Date)
location: class String

CSV 文件如下所示:

Date       Open  High  Low  Close  
31/10/2016 58.25 58.65 58.2 58.35
28/10/2016 58.95 59 58.3 58.35
.
.
.
1/8/2016 50.8 51.1 50.75 50.8

应该如何修改代码以使其工作?

最佳答案

我猜你想要的是这个

SimpleDateFormat hammerFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat slashFormat = new SimpleDateFormat("dd/MM/yyyy");

因此您可以像这样将日期解析为 yyyy-MM-dd 表示形式

hammerFormat.format(slashFormat.parse(date.get(i))));

完整代码

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

public class ReadingTest {
public static void main(String[] args) throws IOException, ParseException {
// Import file to java
File file = new File("table.csv");

// Read the file
Scanner infile = new Scanner(file);

// Skip the first line in table.csv
infile.nextLine();

// Define format of date
SimpleDateFormat hammerFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat slashFormat = new SimpleDateFormat("dd/MM/yyyy");

// Name the variables user enters
Date start = hammerFormat.parse(args[0]);
Date end = hammerFormat.parse(args[1]);

// Create ArrayList for each column of data
ArrayList < String > date = new ArrayList < String > ();
ArrayList < Double > open = new ArrayList < Double > ();
ArrayList < Double > high = new ArrayList < Double > ();
ArrayList < Double > low = new ArrayList < Double > ();
ArrayList < Double > close = new ArrayList < Double > ();

while (infile.hasNext()) {
// Tokenize columns by comma
String[] data = infile.nextLine().split(",");
// Organize each column of data to one index of data array
date.add(data[0]);
open.add(Double.parseDouble(data[1]));
high.add(Double.parseDouble(data[2]));
low.add(Double.parseDouble(data[3]));
close.add(Double.parseDouble(data[4]));
}
// Show options and ask user to choose
System.out.println("1. Hammer");
System.out.println("2. Three white soldiers");
System.out.println("3. Bullish kicker");

// Record user input and execute corresponding code
Scanner input = new Scanner(System.in);
int choice = input.nextInt();

if (choice == 1)
for (int i = 0; i < date.size(); i++)
if (close.get(i) > open.get(i) && close.get(i) > ((high.get(i)) + (low.get(i))) / 2 && ((close.get(i) - low.get(i)) / 2 > (high.get(i) - close.get(i))))
System.out.println("Pattern found: " + hammerFormat.format(slashFormat.parse(date.get(i))));
}
}

编辑:

像这样的 .csv 文件格式(因为在代码中它说 .split(","))

31/10/2016, 58.25, 58.65, 58.20, 58.35
28/10/2016, 58.95, 59.00, 58.30, 58.35
01/08/2016, 50.80, 51.10, 50.75, 50.80

它对我来说很好用。我在执行程序时传递了两个参数2016-09-03 2016-10-31

关于java - 在if语句中解析日期并仅显示规定时间范围内的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40469209/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com