1. 程式人生 > >從新浪獲取恆生指數,獲取新浪財經恆生指數資料

從新浪獲取恆生指數,獲取新浪財經恆生指數資料

獲取恆生指數方法

package cn.com.test.utils.hangseng;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.SocketException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.test.utils.job.GetWeatherJob;


public class GetCsvDataFromSina {
	public final static Log logger = LogFactory.getLog(GetCsvDataFromSina.class); 
    public static final String Sina_FINANCE_URL = "http://hq.sinajs.cn/list=";
    /**
     * 
     * @param stockName 恆生指數-int_hangseng
     * @return var hq_str_int_hangseng=恆生指數,30785.820,197.780,0.650%;
     * @throws Exception
     */
    public static String getStockTodayData(String stockName)
        throws Exception {
        String result = "";

        String url = Sina_FINANCE_URL + stockName ;

        //=======================================
        //  從URL獲取資料
        //=========================================
        URL MyURL = null;
        URLConnection con = null;
        InputStreamReader ins = null;
        BufferedReader in = null;

        try {
            MyURL = new URL(url);
            con = MyURL.openConnection();
            ins = new InputStreamReader(con.getInputStream(), "GBK");
            in = new BufferedReader(ins);
            result = in.readLine();

            // http response返回的字串中,日期包含 雙引號“ 必須刪掉。
            if (result != null) {
                result = result.replace("\"", "");
            }
        } catch (SocketException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (in != null) {
                in.close();
            }
        }
        
        logger.info(result);
        return result;
    }

    static double parseDouble(String data) {
        double result = 0.0;

        try {
            if ((data == null) || (data.trim().length() == 0)) {
                result = Double.valueOf(0.0);
            } else {
                result = Double.valueOf(data.trim());
            }
        } catch (NumberFormatException ex) {
            ex.printStackTrace();
            System.out.print(data);
        }

        return result;
    }
    public static String getRealDate(String beforedate) {
    	DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    	String rtnstr = "";
    	String tempstr[] = beforedate.split("/");
    	if (tempstr.length<3) {
    		rtnstr = df.format(new Date());//返回串中的日期格式異常,設定日期為當前日期
    	} else {
    		String year = tempstr[2];
    		String month = tempstr[0];
    		String day = tempstr[1];
    		if(Integer.parseInt(month)<10) {
    			month = "0"+month;
    		}
    		if(Integer.parseInt(day)<10) {
    			day = "0" + day;
    		}
    		rtnstr = year+"-"+month+"-"+day;
    	}
    	return rtnstr;
    }
    public String getRealData () {
        String rtnstr = "";
		try {
			String	csvData = GetCsvDataFromSina.getStockTodayData("int_hangseng").trim();
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			//HSI%%DATE
			rtnstr = (csvData.split("=")[1].split(",")[1])+"%%"+sdf.format(new Date()).toString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return rtnstr;    	
    }
    
    public static void main(String[] args) throws Exception {
    	GetCsvDataFromSina gy = new GetCsvDataFromSina();
        // 滬市字尾名.ss 例子: 滬深300 000300.ss ,深市字尾名 .sz 例子: 399106.sz
    	String realdata = gy.getRealData();
    	System.out.println(realdata);
        System.exit(0);
    }
}