当前位置 : 主页 > 编程语言 > java >

使用Java做后天处理(导入,计算,统计和分析各种股票数据),使用Delphi或VC做界

来源:互联网 收集:自由互联 发布时间:2021-06-28
处理股票数据的Data类 package cn.implement;import java.math.BigDecimal;import java.sql.SQLException;import java.util.Date;import java.util.HashMap;import java.util.Map;import cn.com.CommonUtils;import cn.com.DESUtils;import cn.com.Da
处理股票数据的Data类
package cn.implement;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import cn.com.CommonUtils;
import cn.com.DESUtils;
import cn.com.DateUtils;
import cn.com.JsonUtils;
import cn.db.AllDetailStockDao;
import cn.db.AllStockDao;
import cn.db.DailyStockDao;
import cn.db.HistoryStockDao;
import cn.db.OperationDao;
import cn.db.OriginalStockDao;
import cn.db.StatisticStockDao;
import cn.db.bean.AllDetailStock;
import cn.db.bean.AllStock;
import cn.db.bean.DetailStock;
import cn.db.bean.StatisticStock;
import cn.log.Log;

public class OperationData extends BaseData {
	Log log = Log.getLoger();
	
	@Override
	protected void closeDao(OperationDao... daoList) {
		try {
			for (OperationDao dao : daoList) {
				String className = dao.getClass().getSimpleName();
				if (className.equals(DailyStockDao.class.getSimpleName())) {
					if (dailyStockDao != null) {
						dailyStockDao.close();
						dailyStockDao = null;
					}
				} else if (className.equals(StatisticStockDao.class.getSimpleName())) {
					if (statisticStockDao != null) {
						statisticStockDao.close();
						statisticStockDao = null;
					}
				} else if (className.equals(OriginalStockDao.class.getSimpleName())) {
					if (originalStockDao != null) {
						originalStockDao.close();
						originalStockDao = null;
					}
				} else if (className.equals(AllDetailStockDao.class.getSimpleName())) {
					if (allDetailStockDao != null) {
						allDetailStockDao.close();
						allDetailStockDao = null;
					}
				} else if (className.equals(AllDetailStockDao.class.getSimpleName())) {
					if (allInformationStockDao != null) {
						allInformationStockDao.close();
						allInformationStockDao = null;
					}
				} else if (className.equals(AllStockDao.class.getSimpleName())) {
					if (allStockDao != null) {
						allStockDao.close();
						allStockDao = null;
					}
				} else if (className.equals(HistoryStockDao.class.getSimpleName())) {
					if (historyStockDao != null) {
						historyStockDao.close();
						historyStockDao = null;
					}
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			log.loger.error(ex);
		}
	}
	
	protected AllDetailStock getDetailStockFromArray(String[] stockInfoArray) {

		AllDetailStock detailStock = new AllDetailStock();
		detailStock.setStockName(stockInfoArray[0]);
		detailStock.setTodayOpen(Double.valueOf(stockInfoArray[1]));
		detailStock.setYesterdayClose(Double.valueOf(stockInfoArray[2]));
		detailStock.setCurrent(Double.valueOf(stockInfoArray[3]));
		detailStock.setTodayHigh(Double.valueOf(stockInfoArray[4]));
		detailStock.setTodayLow(Double.valueOf(stockInfoArray[5]));
		detailStock.setTradedStockNumber(Long.valueOf(stockInfoArray[8]));
		detailStock.setTradedAmount(Float.valueOf(stockInfoArray[9]));
		detailStock.setStockDate(DateUtils.String2Date(stockInfoArray[30]));
		detailStock.setTradedTime(DateUtils.StringToDateTime(stockInfoArray[30] + " " + stockInfoArray[31]));
		detailStock.setStockCode(stockInfoArray[33]);
		String stockCodeDES = DESUtils.encryptToHex(stockInfoArray[33]);
		detailStock.setStockCodeDES(stockCodeDES);
	    // 计算涨跌幅
	    calculateStockChangeRate(detailStock);
		return detailStock;
	}

	/**
	 * 获取前半年的涨跌次数Json
	 *
	 */
	protected String getPreHalfYearJson(String stockCode) throws SQLException {
		
		Date[] preHalfYearStartEndDate = DateUtils.getPreHalfYear();
		Map
 
   preHalfYearUpDownNumberMap = dailyStockDao.statisticUpDownInDailyStock(stockCode, preHalfYearStartEndDate);
		Map
  
    preHalfYearUpNumberMap = dailyStockDao.statisticUpInDailyStock(stockCode, preHalfYearStartEndDate); Map
   
     preHalfYearDownNumberMap = dailyStockDao.statisticDownInDailyStock(stockCode, preHalfYearStartEndDate); final Integer preHalfYearUpDownNumber = preHalfYearUpDownNumberMap.get(stockCode)==null?0:preHalfYearUpDownNumberMap.get(stockCode).getUpDownNumber(); final Integer preHalfYearUpNumber = preHalfYearUpNumberMap.get(stockCode)==null?0:preHalfYearUpNumberMap.get(stockCode).getUpNumber(); final Integer preHalfYearDownNumber = preHalfYearDownNumberMap.get(stockCode)==null?0:preHalfYearDownNumberMap.get(stockCode).getDownNumber(); Map
    
      preHalfYearUpAndDownMap = new HashMap
     
      (){{put(StatisticStock.PRE_HALF_YEAR_UP_DOWN_NUM, preHalfYearUpDownNumber); put(StatisticStock.PRE_HALF_YEAR_UP_NUM, preHalfYearUpNumber); put(StatisticStock.PRE_HALF_YEAR_DOWN_NUM, preHalfYearDownNumber);}}; String preHalfYearUpAndDownJson = JsonUtils.mapToJson(preHalfYearUpAndDownMap); return preHalfYearUpAndDownJson; } /** * 获取前一周的涨跌次数Json * */ protected String getPreHalfMonthJson(String stockCode) throws SQLException { Date[] preHalfMonthStartEndDate = DateUtils.getPreHalfMonth(); Map
      
        preHalfMonthUpDownNumberMap = dailyStockDao.statisticUpDownInDailyStock(stockCode, preHalfMonthStartEndDate); Map
       
         preHalfMonthUpNumberMap = dailyStockDao.statisticUpInDailyStock(stockCode, preHalfMonthStartEndDate); Map
        
          preHalfMonthDownNumberMap = dailyStockDao.statisticDownInDailyStock(stockCode, preHalfMonthStartEndDate); final Integer preHalfMonthUpDownNumber = preHalfMonthUpDownNumberMap.get(stockCode)==null?0:preHalfMonthUpDownNumberMap.get(stockCode).getUpDownNumber(); final Integer preHalfMonthUpNumber = preHalfMonthUpNumberMap.get(stockCode)==null?0:preHalfMonthUpNumberMap.get(stockCode).getUpNumber(); final Integer preHalfMonthDownNumber = preHalfMonthDownNumberMap.get(stockCode)==null?0:preHalfMonthDownNumberMap.get(stockCode).getDownNumber(); Map
         
           preHalfMonthUpAndDownMap = new HashMap
          
           (){{put(StatisticStock.PRE_HALF_MONTH_UP_DOWN_NUM, preHalfMonthUpDownNumber); put(StatisticStock.PRE_HALF_MONTH_UP_NUM, preHalfMonthUpNumber); put(StatisticStock.PRE_HALF_MONTH_DOWN_NUM, preHalfMonthDownNumber);}}; String preHalfMonthUpAndDownJson = JsonUtils.mapToJson(preHalfMonthUpAndDownMap); return preHalfMonthUpAndDownJson; } /** * 获取前一月的涨跌次数Json * */ protected String getPreOneMonthJson(String stockCode) throws SQLException { Date[] preOneMonthStartEndDate = DateUtils.getPreOneMonth(); Map
           
             preOneMonthUpDownNumberMap = dailyStockDao.statisticUpDownInDailyStock(stockCode, preOneMonthStartEndDate); Map
            
              preOneMonthUpNumberMap = dailyStockDao.statisticUpInDailyStock(stockCode, preOneMonthStartEndDate); Map
             
               preOneMonthDownNumberMap = dailyStockDao.statisticDownInDailyStock(stockCode, preOneMonthStartEndDate); final Integer preOneMonthUpDownNumber = preOneMonthUpDownNumberMap.get(stockCode)==null?0:preOneMonthUpDownNumberMap.get(stockCode).getUpDownNumber(); final Integer preOneMonthUpNumber = preOneMonthUpNumberMap.get(stockCode)==null?0:preOneMonthUpNumberMap.get(stockCode).getUpNumber(); final Integer preOneMonthDownNumber = preOneMonthDownNumberMap.get(stockCode)==null?0:preOneMonthDownNumberMap.get(stockCode).getDownNumber(); Map
              
                preOneMonthUpAndDownMap = new HashMap
               
                (){{put(StatisticStock.PRE_ONE_MONTH_UP_DOWN_NUM, preOneMonthUpDownNumber); put(StatisticStock.PRE_ONE_MONTH_UP_NUM, preOneMonthUpNumber); put(StatisticStock.PRE_ONE_MONTH_DOWN_NUM, preOneMonthDownNumber);}}; String preOneMonthUpAndDownJson = JsonUtils.mapToJson(preOneMonthUpAndDownMap); return preOneMonthUpAndDownJson; } /** * 获取前二月的涨跌次数Json * */ protected String getPreTwoMonthJson(String stockCode) throws SQLException { Date[] preTwoMonthStartEndDate = DateUtils.getPreTwoMonth(); Map
                
                  preTwoMonthUpDownNumberMap = dailyStockDao.statisticUpDownInDailyStock(stockCode, preTwoMonthStartEndDate); Map
                 
                   preTwoMonthUpNumberMap = dailyStockDao.statisticUpInDailyStock(stockCode, preTwoMonthStartEndDate); Map
                  
                    preTwoMonthDownNumberMap = dailyStockDao.statisticDownInDailyStock(stockCode, preTwoMonthStartEndDate); final Integer preTwoMonthUpDownNumber = preTwoMonthUpDownNumberMap.get(stockCode)==null?0:preTwoMonthUpDownNumberMap.get(stockCode).getUpDownNumber(); final Integer preTwoMonthUpNumber = preTwoMonthUpNumberMap.get(stockCode)==null?0:preTwoMonthUpNumberMap.get(stockCode).getUpNumber(); final Integer preTwoMonthDownNumber = preTwoMonthDownNumberMap.get(stockCode)==null?0:preTwoMonthDownNumberMap.get(stockCode).getDownNumber(); Map
                   
                     preTwoMonthUpAndDownMap = new HashMap
                    
                     (){{put(StatisticStock.PRE_TWO_MONTH_UP_DOWN_NUM, preTwoMonthUpDownNumber); put(StatisticStock.PRE_TWO_MONTH_UP_NUM, preTwoMonthUpNumber); put(StatisticStock.PRE_TWO_MONTH_DOWN_NUM, preTwoMonthDownNumber);}}; String preTwoMonthUpAndDownJson = JsonUtils.mapToJson(preTwoMonthUpAndDownMap); return preTwoMonthUpAndDownJson; } /** * 获取前三月的涨跌次数Json * */ protected String getPreThreeMonthJson(String stockCode) throws SQLException { Date[] preThreeMonthStartEndDate = DateUtils.getPreThreeMonth(); Map
                     
                       preThreeMonthUpDownNumberMap = dailyStockDao.statisticUpDownInDailyStock(stockCode, preThreeMonthStartEndDate); Map
                      
                        preThreeMonthUpNumberMap = dailyStockDao.statisticUpInDailyStock(stockCode, preThreeMonthStartEndDate); Map
                       
                         preThreeMonthDownNumberMap = dailyStockDao.statisticDownInDailyStock(stockCode, preThreeMonthStartEndDate); final Integer preThreeMonthUpDownNumber = preThreeMonthUpDownNumberMap.get(stockCode)==null?0:preThreeMonthUpDownNumberMap.get(stockCode).getUpDownNumber(); final Integer preThreeMonthUpNumber = preThreeMonthUpNumberMap.get(stockCode)==null?0:preThreeMonthUpNumberMap.get(stockCode).getUpNumber(); final Integer preThreeMonthDownNumber = preThreeMonthDownNumberMap.get(stockCode)==null?0:preThreeMonthDownNumberMap.get(stockCode).getDownNumber(); Map
                        
                          preThreeMonthUpAndDownMap = new HashMap
                         
                          (){{put(StatisticStock.PRE_THREE_MONTH_UP_DOWN_NUM, preThreeMonthUpDownNumber); put(StatisticStock.PRE_THREE_MONTH_UP_NUM, preThreeMonthUpNumber); put(StatisticStock.PRE_THREE_MONTH_DOWN_NUM, preThreeMonthDownNumber);}}; String preThreeMonthUpAndDownJson = JsonUtils.mapToJson(preThreeMonthUpAndDownMap); return preThreeMonthUpAndDownJson; } /** * 获取前一周的涨跌次数Json * */ protected String getPreOneWeekJson(String stockCode) throws SQLException { Date[] preWeekStartEndDate = DateUtils.getPreOneWeek(); Map
                          
                            preOneWeekUpDownNumberMap = dailyStockDao.statisticUpDownInDailyStock(stockCode, preWeekStartEndDate); Map
                           
                             preOneWeekUpNumberMap = dailyStockDao.statisticUpInDailyStock(stockCode, preWeekStartEndDate); Map
                            
                              preOneWeekDownNumberMap = dailyStockDao.statisticDownInDailyStock(stockCode, preWeekStartEndDate); final Integer preOneWeekUpDownNumber = preOneWeekUpDownNumberMap.get(stockCode)==null?0:preOneWeekUpDownNumberMap.get(stockCode).getUpDownNumber(); final Integer preOneWeekUpNumber = preOneWeekUpNumberMap.get(stockCode)==null?0:preOneWeekUpNumberMap.get(stockCode).getUpNumber(); final Integer preOneWeekDownNumber = preOneWeekDownNumberMap.get(stockCode)==null?0:preOneWeekDownNumberMap.get(stockCode).getDownNumber(); Map
                             
                               preOneWeekUpAndDownMap = new HashMap
                              
                               (){{put(StatisticStock.PRE_ONE_WEEK_UP_DOWN_NUM, preOneWeekUpDownNumber); put(StatisticStock.PRE_ONE_WEEK_UP_NUM, preOneWeekUpNumber); put(StatisticStock.PRE_ONE_WEEK_DOWN_NUM, preOneWeekDownNumber);}}; String preOneWeekUpAndDownJson = JsonUtils.mapToJson(preOneWeekUpAndDownMap); return preOneWeekUpAndDownJson; } /** * 获取前一年的涨跌次数Json * */ protected String getPreOneYearJson(String stockCode) throws SQLException { Date[] preOneYearStartEndDate = DateUtils.getPreOneYear(); Map
                               
                                 preOneYearUpDownNumberMap = dailyStockDao.statisticUpDownInDailyStock(stockCode, preOneYearStartEndDate); Map
                                
                                  preOneYearUpNumberMap = dailyStockDao.statisticUpInDailyStock(stockCode, preOneYearStartEndDate); Map
                                 
                                   preOneYearDownNumberMap = dailyStockDao.statisticDownInDailyStock(stockCode, preOneYearStartEndDate); final Integer preOneYearUpDownNumber = preOneYearUpDownNumberMap.get(stockCode)==null?0:preOneYearUpDownNumberMap.get(stockCode).getUpDownNumber(); final Integer preOneYearUpNumber = preOneYearUpNumberMap.get(stockCode)==null?0:preOneYearUpNumberMap.get(stockCode).getUpNumber(); final Integer preOneYearDownNumber = preOneYearDownNumberMap.get(stockCode)==null?0:preOneYearDownNumberMap.get(stockCode).getDownNumber(); Map
                                  
                                    preOneYearUpAndDownMap = new HashMap
                                   
                                    (){{put(StatisticStock.PRE_ONE_YEAR_UP_DOWN_NUM, preOneYearUpDownNumber); put(StatisticStock.PRE_ONE_YEAR_UP_NUM, preOneYearUpNumber); put(StatisticStock.PRE_ONE_YEAR_DOWN_NUM, preOneYearDownNumber);}}; String preOneYearUpAndDownJson = JsonUtils.mapToJson(preOneYearUpAndDownMap); return preOneYearUpAndDownJson; } /** * 计算股票的涨跌幅 * @return * */ protected 
                                    
                                      void calculateStockChangeRate(T stock) { Double yesterdayClose = stock.getYesterdayClose(); if (!CommonUtils.isZeroOrNull(yesterdayClose)) { double current = stock.getCurrent().doubleValue(); double changeRate = ((current-yesterdayClose)*100)/yesterdayClose; BigDecimal bigValue = new BigDecimal(changeRate); double dValue = bigValue.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); stock.setChangeRate(dValue); String changeRateDES = DESUtils.encryptToHex(String.valueOf(dValue)); stock.setChangeRateDES(changeRateDES); } } /** * 根据流通股和交易股计算股票的换手率 * */ protected 
                                     
                                       void calculateTurnoverRate(T stock) throws SQLException { String stockCode = stock.getStockCode(); AllStock allStock = allStockDao.getAllStockByStockCode(stockCode); Long circulationStock = null; if (allStock != null) circulationStock = allStock.getCirculationStockComplex(); if (circulationStock!=null && circulationStock!=0) { Long tradedStockNum = stock.getTradedStockNumber(); Double turnoverRate = (tradedStockNum.doubleValue()/circulationStock.doubleValue())*100; turnoverRate = new BigDecimal(turnoverRate).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); String turnoverRateDES = DESUtils.encryptToHex(turnoverRate.toString()); stock.setTurnoverRate(turnoverRate); stock.setTurnoverRateDES(turnoverRateDES); } } /** * 设置StatisticStock对象的涨跌次数和涨跌次数Json字符串 * */ protected void setUpDownNumberAndUpDownJson(StatisticStock statistic) throws SQLException { String stockCode = statistic.getStockCode(); Integer upNumber = dailyStockDao.getUpNumberByStockCode(stockCode); Integer downNumber = dailyStockDao.getDownNumberByStockCode(stockCode); Integer upDownNumber = dailyStockDao.getUpDownNumberByStockCode(stockCode); String preOneWeekJson = this.getPreOneWeekJson(stockCode); String preHalfMonthJson = this.getPreHalfMonthJson(stockCode); String preOneMonthJson = this.getPreOneMonthJson(stockCode); String preTwoMonthJson = this.getPreTwoMonthJson(stockCode); String preThreeMonthJson = this.getPreThreeMonthJson(stockCode); String preHalfYearJson = this.getPreHalfYearJson(stockCode); String preOneYearJson = this.getPreOneYearJson(stockCode); statistic.setUpDownNumber(upDownNumber); statistic.setUpNumber(upNumber); statistic.setDownNumber(downNumber); statistic.setOneWeek(preOneWeekJson); statistic.setHalfMonth(preHalfMonthJson); statistic.setOneMonth(preOneMonthJson); statistic.setTwoMonth(preTwoMonthJson); statistic.setThreeMonth(preThreeMonthJson); statistic.setHalfYear(preHalfYearJson); statistic.setOneYear(preOneYearJson); } }
                                     
                                    
                                   
                                  
                                 
                                
                               
                              
                             
                            
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 
项目通用工具类CommonUtils.java文件
package cn.com;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import cn.db.bean.BaseStock;
import cn.log.Log;

public class CommonUtils {
	static Log log = Log.getLoger();
	
	public static boolean isEnd(String param) {
		if ("end".equalsIgnoreCase(param)) {
			return true;
		} else {
			return false;
		}
	}
	
	public static boolean isBlank(String obj) {
		if (obj==null || obj.trim().equals("")) 
			return true;
		else 
			return false;
	}

	public static boolean isBlank(List
 
   duplicateStockList) {
		if (duplicateStockList==null || duplicateStockList.size()==0)
			return true;
		else
			return false;
	}
	
	public static List
  
    getDuplicateStocks(String[] stockArray) { List
   
     duplicateStockList = new ArrayList
    
     (); List
     
       singleStockList = new ArrayList
      
       (); for(String stockCode : stockArray) { if(singleStockList.contains(stockCode)) duplicateStockList.add(stockCode); else singleStockList.add(stockCode); } return duplicateStockList; } public static String getDuplicateMessage(List
       
         duplicateStockList) { String stockCodesMessage = ""; for (String stockCode : duplicateStockList) { stockCodesMessage += ", " + stockCode + "(" + PropertiesUtils.getProperty(stockCode) + ")"; } stockCodesMessage = stockCodesMessage.substring(1); return stockCodesMessage; } public static String getActualStockName(List
        
          actualStockList) { String result = ""; for (String[] stock : actualStockList) { result += "," + stock[0] + "(" + stock[2] + ")"; } return result.substring(1, result.length()); } public static String getErrorStockName(List
         
           errorStockList) { String result = ""; for (String[] stock : errorStockList) { result += "," + stock[0] + "(" + stock[1] + ")"; } return result.substring(1, result.length()); } public static String getErrorStockMessage(List
          
            errorStockList) { String result = ""; for (String[] stock : errorStockList) { result += "," + stock[0] + "(" + PropertiesUtils.getProperty(stock[0]) + "):" + stock[1]; } return result.substring(1, result.length()); } public static String getActualStockMessage(List
           
             actualStockList) { String result = ""; for (String[] stock : actualStockList) { result += "," + stock[0] + "(" + PropertiesUtils.getProperty(stock[0]) + "):" + stock[2]; } return result.substring(1, result.length()); } public static List
            
              listErrorStockName(String[] codeArray, String[] nameArray) { List
             
               errorStockList = new ArrayList
              
               (); for (int index=0; index
               
                 codeList = new ArrayList
                
                 (); for (String stockCode : codeArray) { String stockName = PropertiesUtils.getProperty(stockCode); if (CommonUtils.isBlank(stockName)) codeList.add(stockCode); } if (codeList.size() != 0) { return StringUtils.join(codeList.toArray(), ","); } return null; } public static String checkDate(String startDate, String endDate) { if (!isValidDate(startDate)) { return "输入的开始日期格式不正确!"; } if (!isValidDate(endDate)) { return "输入的结束日期格式不正确!"; } return null; } public static String inspectDate(String startDate, String endDate) { if (startDate.trim().equals("") && endDate.trim().equals("")) { return null; } if ((startDate.trim().equals("") && !endDate.trim().equals("")) || !startDate.trim().equals("") && endDate.trim().equals("")) { return "输入的开始日期和结束日期同时为空,或者同时不为空!"; } if (!isValidDate(startDate)) { return "输入的开始日期格式不正确!"; } if (!isValidDate(endDate)) { return "输入的结束日期格式不正确!"; } return null; } /** * 判断时间格式必须为"YYYY-MM-dd" * */ public static boolean isValidDate(String sDate) { DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); try{ Date date = (Date)formatter.parse(sDate); return sDate.equals(formatter.format(date)); }catch(Exception e){ return false; } } /** * 将字符串编码成16进制数字 * */ public static String stringToHex(String str) { char[] chars = "0123456789ABCDEF".toCharArray(); StringBuilder sb = new StringBuilder(""); byte[] bs = str.getBytes(); int bit; for (int i = 0; i < bs.length; i++) { bit = (bs[i] & 0x0f0) >> 4; sb.append(chars[bit]); bit = bs[i] & 0x0f; sb.append(chars[bit]); } return sb.toString().trim(); } /** * 16进制直接转换成为字符串 * */ public static String hexToString(String hexStr) { String str = "0123456789ABCDEF"; char[] hexs = hexStr.toCharArray(); byte[] bytes = new byte[hexStr.length() / 2]; int n; for (int i = 0; i < bytes.length; i++) { n = str.indexOf(hexs[2 * i]) * 16; n += str.indexOf(hexs[2 * i + 1]); bytes[i] = (byte) (n & 0xff); } return new String(bytes); } public static boolean compareDate(Date date1, Date date2) { if (date1==null || date2==null) return false; if (date1.getTime() == date2.getTime()) { return true; } else { return false; } } /** * 将bytes数组转换成十六进制 * */ public static String bytesToHexString(byte[] src) { StringBuilder stringBuilder = new StringBuilder(""); if (src == null || src.length <= 0) { return null; } for (int i = 0; i < src.length; i++) { int v = src[i] & 0xFF; String hv = Integer.toHexString(v); if (hv.length() < 2) { stringBuilder.append(0); } stringBuilder.append(hv); } return stringBuilder.toString(); } /** * 将十六进制转换成bytes数组 * */ public static byte[] hexStringToBytes(String hexString) { if (hexString == null || hexString.equals("")) { return null; } hexString = hexString.toUpperCase(); int length = hexString.length() / 2; char[] hexChars = hexString.toCharArray(); byte[] d = new byte[length]; for (int i = 0; i < length; i++) { int pos = i * 2; d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1])); } return d; } /** * 根据字符串获得需要补充的空格串 * */ public static String getSpaceByString(String stockName) { String spaces = ""; int nameLength = getByteLength(stockName); for (int index=0; index<(8-nameLength); index++) { spaces += " "; } return spaces; } /** * 判断一个字符串的字节长度 * */ public static int getByteLength(String value) { int valueLength = 0; String chinese = "[\u4e00-\u9fa5]"; for (int i = 0; i < value.length(); i++) { String temp = value.substring(i, i + 1); if (temp.matches(chinese)) { valueLength += 2; } else { valueLength += 1; } } return valueLength; } private static byte charToByte(char c) { return (byte) "0123456789ABCDEF".indexOf(c); } public static String supplyNumber(int num, int size) { String result = ""; int numBit = String.valueOf(num).length(); int sizeBit = String.valueOf(size).length(); int differ = sizeBit - numBit; for (int index=0; index
                 
                   call = new Callable
                  
                   () { public String call() throws Exception { String urlRequestInfo = getURLRequestInfo(stockCode); return urlRequestInfo; } }; boolean successFlg = false; String requestInfo = null; try { System.out.print(DateUtils.DateTimeToString(new Date()) + "--获取股票(" + stockCode + ")的url信息..."); Future
                   
                     future = exec.submit(call); // set method getURLRequestInfo timeout to 20 seconds requestInfo = future.get(1000*20, TimeUnit.MILLISECONDS); successFlg = true; } catch (Exception ex) { ex.printStackTrace(); log.loger.error(ex); } exec.shutdown(); if (successFlg) System.out.print("获取url信息成功!"); else System.out.print("获取url信息失败!"); return requestInfo; } @SuppressWarnings({ "deprecation", "resource" }) public static String getURLRequestInfo(String stockCode) { String returnInfo = null; HttpGet request = null; HttpClient client = null; HttpResponse response = null; try { String searchStockCode = getSearchStockCode(stockCode); String url = PropertiesUtils.getProperty("stockURL") + searchStockCode; request = new HttpGet(url); client = new DefaultHttpClient(); response = client.execute(request); // 得到结果,进行解释 int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == 200) { // 连接成功 String strResponse = EntityUtils.toString(response.getEntity(), HTTP.UTF_8); String stockInfo = strResponse.substring(strResponse.indexOf('"') + 1, strResponse.lastIndexOf('"'))+","+stockCode; String[] stockInfoArray = stockInfo.split(","); if (stockInfoArray.length == 34) { returnInfo = stockInfo; } else { System.out.println("股票(" + stockCode + ")数据URL请求连接失败,返回值为: " + stockInfo); log.loger.info("股票(" + stockCode + ")数据URL请求连接失败,返回值为: " + stockInfo); } } else { System.out.println("股票(" + stockCode + ")获取状态码为:" + statusCode + ", URL连接失败!"); log.loger.info("股票(" + stockCode + ")获取状态码为:" + statusCode + ", URL连接失败!"); } } catch (ClientProtocolException ex) { System.out.println("股票(" + stockCode + ")数据URL请求出现异常!"); log.loger.info("股票(" + stockCode + ")数据URL请求出现异常!"); ex.printStackTrace(); log.loger.error(ex); } catch (IOException ex) { System.out.println("股票(" + stockCode + ")数据URL请求出现异常!"); log.loger.info("股票(" + stockCode + ")数据URL请求出现异常!"); ex.printStackTrace(); log.loger.error(ex); } finally { if (request != null) { request.abort(); request = null; } if (client != null) client = null; if (response != null) response = null; } return returnInfo; } private static String getSearchStockCode(String stockCode) { String searchStockCode = ""; if (stockCode.substring(0, 1).equals("6")) { searchStockCode = "sh" + stockCode; } else { searchStockCode = "sz" + stockCode; } return searchStockCode; } public static String getStockCodeByAliasCode(String stockAliasCode) { String stockCode = null; if (stockAliasCode.contains("SH") || stockAliasCode.contains("SZ")) stockCode = stockAliasCode.substring(2); else stockCode = stockAliasCode; return stockCode; } /** * List转换成Map * */ public static Map
                    
                      convertListToMap(List
                      list) { Map
                     
                       map = new HashMap
                      
                       (); for (BaseStock stock : list) { map.put(stock.getStockCode(), stock); } return map; } /* public static Map
                       
                         convertListToMap(List
                        
                          list) { Map
                         
                           map = new HashMap
                          
                           (); for (StatisticStock stock : list) { map.put(stock.getStockCode(), stock); } return map; }*/ public static boolean isLegalDate(String sDate, Date maxStockDate) { Date limitDate = DateUtils.String2Date("2015-01-01"); if (maxStockDate != null) limitDate = maxStockDate; Date stockDate = DateUtils.String2Date(sDate); if (stockDate.compareTo(limitDate) > 0) return true; else return false; } }
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 
上一篇:App轮播效果
下一篇:客服电话进入
网友评论