2015-11-13 53 views
1

所以我在這裏有一些麻煩 - 我試圖創建一個類從雅虎下載歷史股市數據。基本上,在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); 
     } 
    } 
+0

爲什麼使用GregorianCalendar實例來保存日期? – Nyavro

+0

我對Java很陌生,所以我不確定 - 如果你能建議一個更好的方法,請做:) –

回答

2

,可以儲存在名單的日期不是的GregorianCalendar但日期:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
... 
dates.add(format.parse(splitLine[0])); 

的SimpleDateFormat可以幫助你日期格式:

List<Date> dates = new ArrayList<>() 

然後你就可以用SimpleDateFormat的解析日期例如:

SimpleDateFormat newFormat = new SimpleDateFormat("dd-MM-yyyy HH"); //another format 
String formattedDate = newFormat.format(date); //14-11-2015 11 

http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html http://docs.oracle.com/javase/7/docs/api/java/util/Date.html

+0

當構建URL時,我該如何處理每個元素(現在是什麼Calendar.Month等等。 ..) –

+0

對不起,我沒有得到你的問題。那麼你怎麼樣每個元素是什麼?我編輯了我的答案,也許這會有所幫助 – Nyavro

0

使用univocity-parsers爲您處理此。這將是更快的輸入流在一個單獨的線程處理,你的代碼更易於維護:

public StockDownload(String symbol, GregorianCalendar start, GregorianCalendar end) { 
    //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"; 

    //Csv parser configuration - many options here, check the tutorial 
    CsvParserSettings settings = new CsvParserSettings(); 
    //there's headers in the input, let's use them 
    settings.setHeaderExtractionEnabled(true); 

    //You want the data split into columns, so let's use a column processor for that 
    ObjectColumnProcessor columnProcessor = new ObjectColumnProcessor(); 
    //here we assign a conversion to the fields you are interested in getting. 
    columnProcessor.convertFields(Conversions.toCalendar("yyyy-mm-dd")).add("Date"); 
    columnProcessor.convertFields(Conversions.toDouble()).add("Open","High","Low","Close","Adj Close"); 
    columnProcessor.convertFields(Conversions.toInteger()).add("Volume"); 

    //Let's tell the parser to submit all parsed rows to the column processor. 
    settings.setRowProcessor(columnProcessor); 
    //Creates a CSV parser with our configuration 
    CsvParser parser = new CsvParser(settings); 

    try { 
     URL yhoofin = new URL(url); //creates URL from String url 
     URLConnection data = yhoofin.openConnection(); //invokes openConnection method on URL 

     //opens the connection and parses everything. All rows are sent to the the column processor. 
     parser.parseAll(new InputStreamReader(data.getInputStream())); 
    } catch (Exception e) { //catch any error (exception) that happens 
     System.err.println(e); 
    } 

    //Parsing is done. Let's just get the values. 
    Map<String, List<Object>> columns = columnProcessor.getColumnValuesAsMapOfNames(); 

    //Each header in the input is a key in the map. Here we get the list of values for the "close" column. 
    System.out.println(columns.get("Close")); 
} 

披露:我是這個庫的作者。它是開放源代碼和免費的(Apache V2.0許可證)。