标签:style blog io ar color sp java for strong
Calendar 类中是以周日为开始的第一天的,所以Calendar.DAY_OF_WEEK为1的时候是周日.
在股票中有日K 周K和月K的数据.
在此之中的周K是指交易日中一周的数据,周六到周五为一个周期.
1 /** 2 * 返回2个日期间有多少股票周 3 * @param startDate 2012-02-01 开始日期 4 * @param endDate 2014-02-01 结束日期 5 * @return 6 */ 7 public static int getStockWeeks(String startDate,String endDate){ 8 int a = 0; 9 try { 10 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 11 Calendar before = Calendar.getInstance(); 12 Calendar after = Calendar.getInstance(); 13 Calendar temp = Calendar.getInstance(); 14 15 before.setTime(sdf.parse(startDate)); 16 after.setTime(sdf.parse(endDate)); 17 18 long time1 = before.getTime().getTime(); 19 long time2 = after.getTime().getTime(); 20 21 if(time1>time2){//开始和结束时间对换了(有可能前后时间给错了) 22 temp=before; 23 before=after; 24 after=temp; 25 } 26 27 int week = before.get(Calendar.DAY_OF_WEEK); 28 29 before.add(Calendar.DATE, -week);//并非周一为第一天 周六为第一天 30 31 week = after.get(Calendar.DAY_OF_WEEK); 32 33 if(week>0&&week<7)after.add(Calendar.DATE, 6 - week); 34 if(week==7)after.add(Calendar.DATE, 6); 35 a= (int) ((after.getTimeInMillis() - before 36 .getTimeInMillis()) / CONST_WEEK); 37 a = a - 1; 38 if (a == 0) { 39 a = 1; 40 } 41 } catch (Exception e) { 42 e.printStackTrace(); 43 } 44 return a; 45 } 46
java 中获取2个时间段中所包含的周数(股票的周数->从周六到周五)
标签:style blog io ar color sp java for strong
原文地址:http://www.cnblogs.com/china-soft/p/4131311.html