Search code examples
javaarraylistgregorian-calendar

Java Weird System output for GregorianCalendar arraylist


Im currently attempting to have data received from Yahoo finance API saved into different array lists but am having a bit of trouble

package Stocks;

   import java.text.*;
   import java.util.*;
   import java.net.URL;
   import java.net.URLConnection;
   import java.util.Calendar;
   import java.util.Collection;
   import java.util.Date;
   import java.util.GregorianCalendar;
   import java.util.Scanner;
   import java.util.Arrays;


public class StockDownload {

public static final int DATE = 0;
public static final int OPEN = 1;
public static final int HIGH = 2;
public static final int LOW = 3;
public static final int CLOSE = 4;
public static final int VOLUME = 5;
public static final int ADJUSTED = 6;

private ArrayList<GregorianCalendar>dates;
private ArrayList<Double>opens;
private ArrayList<Double>highs;
private ArrayList<Double>lows;
private ArrayList<Double>closes;
private ArrayList<Integer>volumes;
private ArrayList<Double>adjcloses;


public StockDownload(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>();

String url = "http://ichart.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 yahoofin = new URL(url);
    URLConnection data = yahoofin.openConnection();
    Scanner input = new Scanner(data.getInputStream());
    if(input.hasNext())
        input.nextLine();

    while(input.hasNextLine()){
        String line = input.nextLine();

        String info[] = line.split(",");    
        dates.add(getDates(info[0]));
    }
    for(int i = 0; i < dates.size(); i++){
    System.out.println(dates.get(i));

    }

}catch(Exception e){
    System.err.println(e);
}

}   
public GregorianCalendar getDates(String date){

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    Date rawdate = null;
    try {
        rawdate = df.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
    cal.setTime(rawdate);

    GregorianCalendar cleandate = cal;


    return cleandate;
}
}

the output im getting looks like this: java.util.GregorianCalendar[time=1344229200000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/Chicago",offset=-21600000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/Chicago,offset=-21600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2012,MONTH=7,WEEK_OF_YEAR=32,WEEK_OF_MONTH=2,DAY_OF_MONTH=6,DAY_OF_YEAR=219,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=-21600000,DST_OFFSET=3600000]

which i suppose is technically correct, just more info than I need.

I tested out just printing out what gets stored in the info[] index, Im just having trouble shoving the data into an arraylist. Any assistance or a reference to some good reading material would be greatly appreciated! thanks

PS the class that initiates this isnt shown but all it does is pass the requisite variables to the StockDownload method


Solution

  • You want to use the same SimpleDateFormat to print then,

    System.out.println(dates.get(i)); //<-- change this
    

    to something like

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    System.out.println(df.format(dates.get(i).getTime()));