поэтому у меня возникли некоторые проблемы - я пытаюсь создать класс для загрузки исторических данных фондового рынка из Yahoo. По сути, в строке 64 мне нужно разобрать строку вида гггг-мм-дд в тип GregorianCalendar. Я уже некоторое время пытаюсь и смотрю на другие решения здесь и в других местах - хотя я могу проанализировать строку в григорианском календаре, я не могу заставить ее добавлять даты в ArrayList в той же форме yyyy-MM-dd. Я использую .split(,) для разделения каждой строки csv на отдельные элементы, а все остальные типы — это Doubles и Ints, что достаточно просто.
строка возвращает строку, такую как: 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);
}
}