所以我在这里有一些麻烦 - 我试图创建一个类从雅虎下载历史股市数据。基本上,在64行我需要解析一个格式为yyyy-MM-dd的字符串为GregorianCalendar类型。我一直在尝试一段时间,看看这里和其他地方的其他解决方案 - 虽然我可以将字符串解析为公历,但我无法用相同的格式yyyy-MM-dd将它添加到ArrayList日期。我正在使用.split(,)将csv的每一行分割为单独的元素,而其他所有类型都是Doubles和Ints,这很容易。麻烦分析字符串GregoriamCalendar类型使用ArrayList
线返回一个字符串,如:事先 2015-11-12,116.260002,116.82,115.650002,115.720001,32262600,115.720001
谢谢!
public StockDownloader(String symbol, GregorianCalendar start, GregorianCalendar end) {
dates = new ArrayList<GregorianCalendar>();
opens = new ArrayList<Double>();
highs = new ArrayList<Double>();
lows = new ArrayList<Double>();
closes = new ArrayList<Double>();
volumes = new ArrayList<Integer>();
adjCloses = new ArrayList<Double>();
//deconstructed URL
String url = "http://real-chart.finance.yahoo.com/table.csv?s="+symbol+
"&a="+start.get(Calendar.MONTH)+
"&b="+start.get(Calendar.DAY_OF_MONTH)+
"&c="+start.get(Calendar.YEAR)+
"&d="+end.get(Calendar.MONTH)+
"&e="+end.get(Calendar.DAY_OF_MONTH)+
"&f="+end.get(Calendar.YEAR)+
"&g=d&ignore=.csv";
try {
URL yhoofin = new URL(url); //creates URL from String url
URLConnection data = yhoofin.openConnection(); //invokes openConnection method on URL
Scanner input = new Scanner(data.getInputStream()); //Returns an input stream that reads from this open connection.
if(input.hasNext()) //skip line, it's just the header
input.nextLine(); //advances to next line
//start reading data
while(input.hasNextLine()) {
String line = input.nextLine();
String[] splitLine = line.split(",");
>>Problem here //dates.add(add the date);
opens.add(Double.parseDouble(splitLine[OPEN]));
highs.add(Double.parseDouble(splitLine[HIGH]));
lows.add(Double.parseDouble(splitLine[LOW]));
closes.add(Double.parseDouble(splitLine[CLOSE]));
volumes.add(Integer.parseInt(splitLine[VOLUME]));
adjCloses.add(Double.parseDouble(splitLine[ADJCLOSE]));
}
}
catch(Exception e) { //catch any error (exception) that happens
System.err.println(e);
}
}
为什么使用GregorianCalendar实例来保存日期? – Nyavro
我对Java很陌生,所以我不确定 - 如果你能建议一个更好的方法,请做:) –