本文主要是介绍从新浪获取恒生指数,获取新浪财经恒生指数数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
获取恒生指数方法
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%%DATErtnstr = (csvData.split("=")[1].split(",")[1])+"%%"+sdf.format(new Date()).toString();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}return rtnstr; }public static void main(String[] args) throws Exception {GetCsvDataFromSina gy = new GetCsvDataFromSina();// 沪市后缀名.ss 例子: 沪深300 000300.ss ,深市后缀名 .sz 例子: 399106.szString realdata = gy.getRealData();System.out.println(realdata);System.exit(0);}
}
这篇关于从新浪获取恒生指数,获取新浪财经恒生指数数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!