所以我在這裏有一些麻煩 - 我試圖創建一個類從雅虎下載歷史股市數據。基本上,在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很陌生,所以我不確定 - 如果你能建議一個更好的方法,請做:) –