调用Cognos sdk接口生成报表
2011-07-20 21:27
260 查看
java调用SDK提供的接口实现生成报表静态文件至本机(html,excel格式)的一个demo
/** * 连接cognos服务器 */ public class CognosServerConnectAL { /** * cognos内容库服务Locater对象 */ private ContentManagerService_ServiceLocator cmServiceLocator; /** * cognos报表服务Locater对象 */ private ReportService_ServiceLocator reportServiceLocator; /** * cognos内容库服务对象 */ private ContentManagerService_Port cmService; /** * cognos报表服务对象 */ private ReportService_Port repService; /** * 获取内容库服务Locater对象 * @return */ public ContentManagerService_ServiceLocator getCmServiceLocator() { return cmServiceLocator; } /** * 设置内容库服务Locater对象 * @param cmServiceLocator */ public void setCmServiceLocator( ContentManagerService_ServiceLocator cmServiceLocator) { this.cmServiceLocator = cmServiceLocator; } /** * 获取报表服务Locater对象 * @return */ public ReportService_ServiceLocator getReportServiceLocator() { return reportServiceLocator; } /** * 设置报表服务Locater对象 * @param reportServiceLocator */ public void setReportServiceLocator( ReportService_ServiceLocator reportServiceLocator) { this.reportServiceLocator = reportServiceLocator; } /** * 获得内容库连接 * * @param cognosUrl cognos服务器URL * @return * @throws ServiceException * @throws MalformedURLException */ public ContentManagerService_Port connectToCognosServer(String cognosUrl) throws ServiceException, MalformedURLException { java.net.URL serverURL = new java.net.URL(cognosUrl); cmService = cmServiceLocator.getcontentManagerService (serverURL); repService = reportServiceLocator.getreportService (serverURL); ((Stub) cmService).setTimeout(0);//内容库超时 ((Stub) repService).setTimeout(0);//报表服务超时 return cmService; } /** * 获得cognos内容库服务对象 * * @return */ public ContentManagerService_Port getCMService() { return cmService; } /** * 获得cognos报表服务对象 * * @return */ public ReportService_Port getReportService() { return repService; } } 注:连接Cognos服务未使用权限。 设置报表参数: /* * PURPOSE : 设置报表参数 */ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.List; import com.cognos.developer.schemas.bibus._3.ParameterValue; import com.cognos.developer.schemas.bibus._3.ParmValueItem; import com.cognos.developer.schemas.bibus._3.SimpleParmValueItem; /** * 报表参数设置 * * @author */ public class SetReportParameterAL { /** * 设置报表所需的所有参数值 * * @param p * @return */ public ParameterValue[] setReportParameters(RepParameterHelper p) { // 获取频率 int repFrequency = p.getReportPutInfoDomain().getReportFreq (); //获取结束日期 Calendar endDate = p.getReportDate(); //获取参数、参数值 List<ReportPropertyDomain> list = p.getProperties(); int propNum = 0; if (endDate != null) { /* 根据期末日期和报表频率获得报告期间的开始日期 */ Calendar startDate = getStartDateByRepFrequency (repFrequency, endDate); if (list != null) { propNum = list.size(); } //报表运行参数对象 ParameterValue[] parameters=null; parameters = new ParameterValue[propNum + 1]; // 固定参数数 组+开始结束日期 ParameterValue parameter = null; ReportPropertyDomain propDomain = null; for (int i = 0; i < propNum; i++) { propDomain = list.get(i); //设置参数名,参数值至参数对象 parameter = setParameter(p, propDomain.getParameterName (),propDomain.getParameterValue()); parameters = parameter; } //设置结束日期 parameters[propNum] = setParameter(p, ParameterUtil.END_DATE, calendarToString(endDate)); //设置开始日期 parameters[propNum + 1] = setParameter(p, ParameterUtil.START_DATE, calendarToString(startDate)); return parameters; } else { return null; } } /** * 根据频率和结束日期,获得开始日期 * * @param repFrequency * 报表频率,(日,周,月,季,半年,年 分别对应 1,2,3,4,5,6) * @param endDate * 结束日期 * @return */ private Calendar getStartDateByRepFrequency(int repFrequency, Calendar endDate) { Calendar startDate = new GregorianCalendar(); startDate.set(Calendar.YEAR, endDate.get(Calendar.YEAR));//设 置年 startDate.set(Calendar.MONTH, endDate.get(Calendar.MONTH));// 设置月 startDate.set(Calendar.DAY_OF_MONTH, endDate.get (Calendar.DAY_OF_MONTH));//设置日 switch (repFrequency) { // 日报的开始日期,开始日期和结束日期同一日 case ParameterUtil.DAY: return startDate; // 周报的开始日期,开始日期为星期一 case ParameterUtil.WEEK: startDate.set(Calendar.DAY_OF_WEEK, 1); break; // 月报的开始日期,开始日期为月初 case ParameterUtil.MONTH: startDate.set(Calendar.DAY_OF_MONTH, 1); break; // 季报的开始日期 case ParameterUtil.SEASON: switch (endDate.get(Calendar.MONTH)) { case Calendar.MARCH://第一季度 startDate.set(Calendar.MONTH, 0);//开始日期,1月1日 startDate.set(Calendar.DAY_OF_MONTH, 1); break; case Calendar.JUNE://第二季度 startDate.set(Calendar.MONTH, 3);//开始日期,4月1日 startDate.set(Calendar.DAY_OF_MONTH, 1); break; case Calendar.SEPTEMBER://第三季度 startDate.set(Calendar.MONTH, 6);//开始日期,7月1日 startDate.set(Calendar.DAY_OF_MONTH, 1); break; case Calendar.DECEMBER://第四季度 startDate.set(Calendar.MONTH, 9);//开始日期,10月1日 startDate.set(Calendar.DAY_OF_MONTH, 1); break; default: } ; break; // 半年报的开始日期 case ParameterUtil.HALF_YEAR: if (endDate.get(Calendar.MONTH) == Calendar.JUNE) {//如果结 束日期为6月份 startDate.set(Calendar.MONTH, 0);//开始日期,1月1日 startDate.set(Calendar.DAY_OF_MONTH, 1); } else { startDate.set(Calendar.MONTH, 6);//开始日期,7月1日 startDate.set(Calendar.DAY_OF_MONTH, 1); } ; break; // 年报的开始日期 case ParameterUtil.YEAR: startDate.set(Calendar.MONTH, 0);//开始日期1月1日 startDate.set(Calendar.DAY_OF_MONTH, 1); break; default: } return startDate; } /** * Calendar转换为日期格式的字符串 * * @param calendar * @return */ private String calendarToString(Calendar calendar) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM- dd"); String date = dateFormat.format(calendar.getTime()); return date; } /** * 设置参数 * * @param paramName * 参数名 * @param paramValue * 参数值 * @return */ private ParameterValue setParameter(RepParameterHelper p, String paramName, String paramValue) { //参数对象 ParameterValue parameter = new ParameterValue(); /* 同个参数名但有一组值(in(a,b,c,d)) */ paramValues = paramValue.split(",");//分割一组值 int paramNum = paramValues.length; ParmValueItem[] pvi = new ParmValueItem[paramNum]; //分割后循环依次设置到参数对象中 for (int i = 0; i < paramNum; i++) { SimpleParmValueItem item = new SimpleParmValueItem(); item.setUse(paramValues);//使用值 pvi = item; } parameter.setName(paramName); parameter.setValue(pvi); return parameter; } }
/* * 运行导出报表 */ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.rmi.RemoteException; import com.cognos.developer.schemas.bibus._3.AsynchDetailReportOutput; import com.cognos.developer.schemas.bibus._3.AsynchDetailReportStatus; import com.cognos.developer.schemas.bibus._3.AsynchDetailReportStatusEnum; import com.cognos.developer.schemas.bibus._3.AsynchOptionEnum; import com.cognos.developer.schemas.bibus._3.AsynchOptionInt; import com.cognos.developer.schemas.bibus._3.AsynchReply; import com.cognos.developer.schemas.bibus._3.AsynchReplyStatusEnum; import com.cognos.developer.schemas.bibus._3.AsynchSecondaryRequest; import com.cognos.developer.schemas.bibus._3.BaseClass; import com.cognos.developer.schemas.bibus._3.ContentManagerService_Port; import com.cognos.developer.schemas.bibus._3.Graphic; import com.cognos.developer.schemas.bibus._3.Option; import com.cognos.developer.schemas.bibus._3.Output; import com.cognos.developer.schemas.bibus._3.OutputEncapsulationEnum; import com.cognos.developer.schemas.bibus._3.ParameterValue; import com.cognos.developer.schemas.bibus._3.PropEnum; import com.cognos.developer.schemas.bibus._3.QueryOptions; import com.cognos.developer.schemas.bibus._3.RunOptionBoolean; import com.cognos.developer.schemas.bibus._3.RunOptionEnum; import com.cognos.developer.schemas.bibus._3.RunOptionLanguageArray; import com.cognos.developer.schemas.bibus._3.RunOptionOutputEncapsulation; import com.cognos.developer.schemas.bibus._3.RunOptionStringArray; import com.cognos.developer.schemas.bibus._3.SearchPathMultipleObject; import com.cognos.developer.schemas.bibus._3.SearchPathSingleObject; import com.cognos.developer.schemas.bibus._3.Sort; /** * 运行报表 * * @author */ public class RunReportAL { /** * 设置报表参数AL */ private SetReportParameterAL setReportParameterAL; public SetReportParameterAL getSetReportParameterAL() { return setReportParameterAL; } public void setSetReportParameterAL( SetReportParameterAL setReportParameterAL) { this.setReportParameterAL = setReportParameterAL; } /** * 运行报表 * * @param connect * 报表服务器连接AL * @param reportSearchPath * 报表搜索路径 * @param reportType * 报表类型:1:XLS 2:HTML * @param parameters * 报表参数helper * @return * @throws IOException */ public String run(CognosServerConnectAL connect, String reportSearchPath, int reportType, RepParameterHelper parameters) throws IOException { String output = null; try{ if ((connect != null) && (reportSearchPath != null)) { /* 通过reportSearchPath【报表存放路劲】获得报表对象 */ BaseClass[] reportDomain = getRoportModel(reportSearchPath, connect .getCMService()); if (reportDomain.length > 0) { ParameterValue[] emptyParameterValues = new ParameterValue[] {}; // 空参 ParameterValue[] reportParameterValues = null;// String compagers = null; String savePath = null; String partOfFileoutPutName = null;//生成报表静态文件名称 if (parameters != null) { // 设置报表参数 reportParameterValues = setReportParameterAL .setReportParameters(parameters); compagers = parameters.getCompagers(); savePath = parameters.getSavePath(); /* 文件名组成:报表ID+报表英文名称 */ partOfFileoutPutName = parameters.getReportPutInfoDomain() .getReportID() + parameters.getReportPutInfoDomain() .getReportEnglishName(); } //如果传入的参数数组为空则数组为null if ((reportParameterValues == null) || (reportParameterValues.length <= 0)) { //emptyParameterValues 上面定义的一组空参(执行报表必须要有参数数组,数组内容可为空) reportParameterValues = emptyParameterValues; } //执行报表 /* reportSearchPath:报表存放地址 connect:连接对象 reportType:报表文件类型(html,excel) reportParameterValues:参数数组 */ output = executeReport(reportSearchPath, connect, reportType, reportParameterValues, compagers, savePath, partOfFileoutPutName); } } return output; }catch (Exception e) { e.printStackTrace(); return output; } } /** * 执行报表 * * @param reportSearchPath * 报表搜索路径 * @param connect * 报表服务器连接AL * @param reportType * 报表类型:1:XLS 2:HTML * @param paramValueArray * 报表参数 * @param compagers * @param savePath * 保存路径 * @param partOfFileoutPutName * 报表英文名称 * @return * @throws IOException */ private String executeReport(String reportSearchPath, CognosServerConnectAL connect, int reportType, ParameterValue[] paramValueArray, String compagers, String savePath, String partOfFileoutPutName) throws IOException { try{ Option[] execReportRunOptions = getRunOptions(reportType); // 报表运行配置 AsynchReply rsr = null; // 报表内容 // main rsr = connect.getReportService().run( new SearchPathSingleObject(reportSearchPath), paramValueArray, execReportRunOptions); // 输出对象没有立刻获得时,执行等待,直到获得输出对象为止 if (!rsr.getStatus().equals(AsynchReplyStatusEnum.complete) && !rsr.getStatus().equals( AsynchReplyStatusEnum.conversationComplete)) { while (!rsr.getStatus().equals(AsynchReplyStatusEnum.complete) && !rsr.getStatus().equals( AsynchReplyStatusEnum.conversationComplete)) { // 执行等待之前,进行确认 if (!hasSecondaryRequest(rsr, "wait")) { return null; } rsr = connect.getReportService().wait(rsr.getPrimaryRequest(), new ParameterValue[] {}, new Option[] {}); } // 确保获得输出对象前,输出对象已经完成提交 if (outputIsReady(rsr)) { rsr = connect.getReportService().getOutput( rsr.getPrimaryRequest(), new ParameterValue[] {}, new Option[] {}); } else { return null; } } // 导出 return textOrBinaryOutput(connect, rsr, partOfFileoutPutName, reportType, compagers, savePath); }catch (Exception e) { e.printStackTrace(); return null; } } /** * 报表运行选项设置 * * @param reportType * @return */ private Option[] getRunOptions(int reportType) { //定义运行报表选项数组 Option[] execReportRunOptions = new Option[7]; //指定是否应该将报表输出保存到内容库中 RunOptionBoolean saveOutputRunOption = new RunOptionBoolean(); //导出文件格式 RunOptionStringArray outputFormat = new RunOptionStringArray(); //是否跳过提示页面 RunOptionBoolean promptFlag = new RunOptionBoolean(); //指定存储输出的位置 RunOptionOutputEncapsulation outputEncapsulation = new RunOptionOutputEncapsulation(); saveOutputRunOption.setName(RunOptionEnum.saveOutput); saveOutputRunOption.setValue(false);//true为保存,false反之 outputFormat.setName(RunOptionEnum.outputFormat); String[] reportFormat = null; reportFormat = setFormatByType(reportType); outputFormat.setValue(reportFormat);//setValue值应为String类型的数组 promptFlag.setName(RunOptionEnum.prompt); promptFlag.setValue(false);//false:跳过提示页面运行报表 //开始之前等待报表完成的初始时间,以秒为单位,默认值为 7 秒 AsynchOptionInt primaryWaitThreshold = new AsynchOptionInt(); primaryWaitThreshold.setName(AsynchOptionEnum.primaryWaitThreshold); primaryWaitThreshold.setValue(20); //在匿名会话的过程中,等待状态检查的时间,以秒为单位 AsynchOptionInt secondaryWait = new AsynchOptionInt(); secondaryWait.setName(AsynchOptionEnum.secondaryWaitThreshold); secondaryWait.setValue(65); //设置语言本地化 RunOptionLanguageArray outputLocale = new RunOptionLanguageArray(); outputLocale.setName(RunOptionEnum.outputLocale); outputLocale.setValue(new String[]{"zh"}); outputEncapsulation.setName(RunOptionEnum.outputEncapsulation); /* 枚举none:输出不存入临时内容库对象中 (如果报表输出的内容较大如果保存至临时内容库会造成内存溢出 */ outputEncapsulation.setValue(OutputEncapsulationEnum.none); //添加各个运行选项 execReportRunOptions[0] = saveOutputRunOption; execReportRunOptions[1] = outputFormat; execReportRunOptions[2] = promptFlag; execReportRunOptions[3] = primaryWaitThreshold; execReportRunOptions[4] = outputLocale; execReportRunOptions[5] = outputEncapsulation; execReportRunOptions[6] = secondaryWait; //返回 选项 数组 return execReportRunOptions; } /** * 设置导出格式 * * @param fileType * 1:XLS 2:HTML * @return */ private String[] setFormatByType(int fileType) { switch (fileType) { case ParameterUtil.XLS: return new String[] { "singleXLS" };//excel格式 case ParameterUtil.HTML: return new String[] { "HTML" };//html格式 default: return new String[] { "singleXLS" }; } /* 格式还可以为CSV , HTMLFragment, MHT, PDF, singleXLS, XHTML, XLS, XLWA, XML */ } /** * 检查响应是否进入了secondaryRequest状态 * * @param response * 报表内容 * @param secondaryRequest * wait * @return */ private boolean hasSecondaryRequest(AsynchReply response, String secondaryRequest) { AsynchSecondaryRequest[] secondaryRequests = response .getSecondaryRequests(); for (int i = 0; i < secondaryRequests.length; i++) { if (secondaryRequests.getName().compareTo(secondaryRequest) == 0) { return true; } } return false; } /** * 输出对象是否已经完全获得 * * @param response * 报表内容 * @return */ private boolean outputIsReady(AsynchReply response) { for (int i = 0; i < response.getDetails().length; i++) { if ((response.getDetails() instanceof AsynchDetailReportStatus) && (((AsynchDetailReportStatus) response.getDetails()) .getStatus() == AsynchDetailReportStatusEnum.responseReady) && (hasSecondaryRequest(response, "getOutput"))) { return true; } } return false; } /** * 生成不同格式的报表 * * @param connect * 报表服务器连接AL * @param rsr * 报表内容 * @param partOfFileoutPutName * 报表英文名称 * @param reportType * 报表类型 * @param compagers * @param savePath * 保存路径 * @return * @throws IOException */ private String textOrBinaryOutput(CognosServerConnectAL connect, AsynchReply rsr, String partOfFileoutPutName, int reportType, String compagers, String savePath) throws IOException { String textOutput = null; if (reportType == ParameterUtil.XLS) { textOutput = saveBinaryOutput(connect, rsr, partOfFileoutPutName, reportType, compagers, savePath); } if (reportType == ParameterUtil.HTML) { textOutput = getOutputPage(connect, rsr, partOfFileoutPutName, reportType, compagers, savePath); } return textOutput; } /** * 导出xls格式的报表,并且写入文件 * * @param connection * @param response * @param partOfFileoutPutName * @param reportType * @param compagers * @param savePath * @return * @throws IOException */ private String saveBinaryOutput(CognosServerConnectAL connection, AsynchReply response, String partOfFileoutPutName, int reportType, String compagers, String savePath) throws IOException { String fileSaveUrl = null; byte[] binaryOutput = null; AsynchDetailReportOutput reportOutput = null; for (int i = 0; i < response.getDetails().length; i++) { if (response.getDetails() instanceof AsynchDetailReportOutput) { reportOutput = (AsynchDetailReportOutput) response.getDetails(); break; } } binaryOutput = (reportOutput.getOutputPages()[0]).getBytes("UTF-8"); if (binaryOutput == null) { return null; } //创建文件 createNewFile(savePath); File oFile = new File(setFilenameByType(savePath, partOfFileoutPutName, reportType, compagers)); FileOutputStream fos = new FileOutputStream(oFile); fos.write(binaryOutput); fos.flush(); fos.close(); //获取绝对路劲 fileSaveUrl = oFile.getAbsolutePath(); return fileSaveUrl; } /** * 导出html格式的报表,并且写入文件 * * @param connect * @param response * @param partOfFileoutPutName * @param reportType * @param compagers * @param savePath * @return * @throws IOException */ private String getOutputPage(CognosServerConnectAL connect, AsynchReply response, String partOfFileoutPutName, int reportType, String compagers, String savePath) throws IOException { String fileSaveUrl = null; String textOutput = ""; AsynchDetailReportOutput reportOutput = null; for (int i = 0; i < response.getDetails().length; i++) { if (response.getDetails() instanceof AsynchDetailReportOutput) { reportOutput = (AsynchDetailReportOutput) response.getDetails(); break; } } if (reportOutput == null) { return null; } if (reportOutput.getOutputObjects().length > 0) { textOutput = replaceLocalGraphicsInOutput(connect, reportOutput, savePath, partOfFileoutPutName, compagers); } else { textOutput = reportOutput.getOutputPages()[0].toString(); } createNewFile(savePath); File oFile = new File(setFilenameByType(savePath, partOfFileoutPutName, reportType, compagers)); FileOutputStream fos = new FileOutputStream(oFile); fos.write(textOutput.getBytes("UTF-8")); fos.flush(); fos.close(); fileSaveUrl = oFile.getAbsolutePath(); return fileSaveUrl; } /** * 包含图表的报表特殊处理 * 这一部分不是很熟悉 * @param connect * @param reportOutput * @param savePath * @param partOfFileoutPutName * @param compages * @return * @throws IOException */ private String replaceLocalGraphicsInOutput(CognosServerConnectAL connect, AsynchDetailReportOutput reportOutput, String savePath, String partOfFileoutPutName, String compages) throws IOException { BaseClass[] bcGraphic; SearchPathMultipleObject graphicSearchPath = new SearchPathMultipleObject(); graphicSearchPath.setValue(reportOutput.getOutputObjects()[0] .getSearchPath().getValue()); bcGraphic = connect.getCMService().query(graphicSearchPath, new PropEnum[] { PropEnum.searchPath }, new Sort[] {}, new QueryOptions()); Output out = null; if ((bcGraphic.length > 0) && (bcGraphic[0] instanceof Output)) { SearchPathMultipleObject outSearchPath = new SearchPathMultipleObject(); out = (Output) bcGraphic[0]; outSearchPath.setValue(out.getSearchPath().getValue() + "/graphic"); BaseClass[] g = connect.getCMService().query( outSearchPath, new PropEnum[] { PropEnum.searchPath, PropEnum.data, PropEnum.dataType }, new Sort[] {}, new QueryOptions()); StringBuffer finalHtml = new StringBuffer(); String[] pages = reportOutput.getOutputPages(); String html = pages[0].toString(); String start = null; String end = null; String graphicFile = null; // 替换html文件中img标签的属性 for (int i = 0; i < g.length; i++) { String pictrueName = partOfFileoutPutName + "_" + compages + i + ".png"; graphicFile = savePath + "/" + pictrueName; // 图表保存本地 File gFile = new File(graphicFile); FileOutputStream fos = new FileOutputStream(gFile); fos.write(((Graphic) g).getData().getValue()); fos.flush(); fos.close(); int index = 0; index = html.indexOf("<img", 0); start = html.substring(0, index); end = html.substring(html.indexOf(">", index) + 1); finalHtml.append(start + "<img src='" + pictrueName + "'>"); html = end; } finalHtml.append(html); return finalHtml.toString(); } return null; } /** * 根据格式设置文件名 * * @param filePath文件路劲 * @param reportName文件名称 * @param fileType文件类型 * @return */ private String setFilenameByType(String filePath, String partOfFileoutPutName, int fileType, String compagers) { switch (fileType) { case ParameterUtil.XLS: return filePath + System.getProperty("file.separator") + partOfFileoutPutName + "_" + compagers + ".xls"; case ParameterUtil.HTML: return filePath + System.getProperty("file.separator") + partOfFileoutPutName + "_" + compagers + ".html"; default: return null; } } /** * 创建目录 * * @param savePath */ private void createNewFile(String savePath) { File file = new File(savePath); if (!file.exists()) { file.mkdirs(); } } /** * 内容库报表对象查询 * * @param searchPath * @return * @throws RemoteException */ private BaseClass[] getRoportModel(String searchPath, ContentManagerService_Port cmService) throws RemoteException { PropEnum[] props = { PropEnum.searchPath }; BaseClass[] bc = null; SearchPathMultipleObject spMulti = new SearchPathMultipleObject(); spMulti.setValue(searchPath); bc = cmService.query(spMulti, props, new Sort[] {}, new QueryOptions()); return bc; } }
/* * 根据报告日期判定报告频率 */ import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; /** * 工作日,周末、月末、季末、半年末,年末的判断 * * */ public class MeasureReportTypeByRepDateAL { private ITjFactDealday tjFactDealday; /** * * @return */ public ITjFactDealday getTjFactDealday() { return tjFactDealday; } /** * * @param tjFactDealday */ public void setTjFactDealday(ITjFactDealday tjFactDealday) { this.tjFactDealday = tjFactDealday; } /** * 判断某日期是否为其所在周、月、季、半年、年的最后一个工作日,即周末,月末,季末,半年末,年末 * * @param reportDate * 报告日期 * @param freq * 频率:2:周,3:月,4:季,5:半年,6:年 * @return */ public boolean isEndDayByFreq(Date reportDate, int freq) { boolean isEndDay = false; Calendar reportDay = dateToCalendar(reportDate); Calendar nextDay_1 = new GregorianCalendar(); // nextDay_1:reportDay的下一天 nextDay_1.set(Calendar.YEAR, reportDay.get(Calendar.YEAR));//下一天 所处的年份 nextDay_1.set(Calendar.DAY_OF_YEAR,reportDay.get(Calendar.DAY_OF_YEAR) + 1); boolean nextDayIsWorkDay = isWorkDay(nextDay_1.getTime());//下一天是否是工作日 Date nextDay_2 = null; // nextDay_2:reportDay的下一个工作日 if (!nextDayIsWorkDay) {//如果下一天非工作日 nextDay_2 = getAfterWorkDay(nextDay_1.getTime());//获取工作日 日期 } else {//下一天为工作日 nextDay_2 = nextDay_1.getTime();//获取工作日 日期 } Calendar nextWorkDay = dateToCalendar(nextDay_2);//转换为Calendar类型 switch (freq) { // 判断当前日期是否为该日期所在自然周的最后一个工作日 case ParameterUtil.WEEK:// 周的常量值:2 //如果报告日期所属当年的周与下一个工作日所属当年的周不相等 if (reportDay.get(Calendar.WEEK_OF_YEAR) != nextWorkDay.get(Calendar.WEEK_OF_YEAR)) { isEndDay = true;//则报告日期为本周最后一个工作日 } ; break; // 判断当前日期是否为该日期所在月的最后一个工作日 case ParameterUtil.MONTH://月的常量值:3 if (reportDay.get(Calendar.MONTH) != nextWorkDay.get(Calendar.MONTH)) { isEndDay = true; } ; break; // 判断当前日期是否为该日期所在季度的最后一个工作日 case ParameterUtil.SEASON://季度的常量值:4 if ((reportDay.get(Calendar.MONTH) == Calendar.MARCH) && (nextWorkDay.get(Calendar.MONTH) != Calendar.MARCH)) { isEndDay = true; } else if ((reportDay.get(Calendar.MONTH) == Calendar.JUNE) && (nextWorkDay.get(Calendar.MONTH) != Calendar.JUNE)) { isEndDay = true; } else if ((reportDay.get(Calendar.MONTH) == Calendar.SEPTEMBER) && (nextWorkDay.get(Calendar.MONTH) != Calendar.SEPTEMBER)) { isEndDay = true; } else if ((reportDay.get(Calendar.MONTH) == Calendar.DECEMBER) && (nextWorkDay.get(Calendar.MONTH) != Calendar.DECEMBER)) { isEndDay = true; } ; break; // 判断当前日期是否为该日期所在半年的最后一个工作日 case ParameterUtil.HALF_YEAR://半年的常量值:5 if ((reportDay.get(Calendar.MONTH) == Calendar.JUNE) && (nextWorkDay.get(Calendar.MONTH) != Calendar.JUNE)) { isEndDay = true; } else if ((reportDay.get(Calendar.MONTH) == Calendar.DECEMBER) && (nextWorkDay.get(Calendar.MONTH) != Calendar.DECEMBER)) { isEndDay = true; } ; break; // 判断当前日期是否为该日期所在年份的最后一个工作日 case ParameterUtil.YEAR://年的常量值:6 if (reportDay.get(Calendar.YEAR) != nextWorkDay.get(Calendar.YEAR)) { isEndDay = true; } ; break; default: } return isEndDay; } /** * 获取reportDate的下一个工作日,注:reportDate必须为非工作日 * 此方法根据自己的需求获取工作日 * @param reportDate * @return */ public Date getAfterWorkDay(Date reportDate) { return tjFactDealday.afterDay(reportDate); } /** * Date类型数据转换为Calendar类型数据 * * @param date * @return */ private Calendar dateToCalendar(Date date) { int year = DamsDateUtils.getYear(date); int month = DamsDateUtils.getMonth(date); int day = DamsDateUtils.getDay(date); Calendar calendar = new GregorianCalendar(); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, month); // Calendar的月份0-11代表1月到12月 calendar.set(Calendar.DAY_OF_MONTH, day); return calendar; } }
相关文章推荐
- 调用Cognos sdk接口生成报表
- java 生成js接口签名 页面调用JS-SDK
- 支付宝支付之“单笔转账到支付宝账户接口”的调用(生成签名、上传应用公钥、下载SDK、接口调用、报错自动排查、查看错误码)
- iOS app支付宝接口调用的一点总结(补充支付宝SDK&Demo下载地址)
- 转售调用接口时生成的流水号
- php中 如何生成jsonp数据接口及如何调用?
- 微信开放平台 公众号第三方平台开发 教程四 代公众号调用接口的SDK和demo
- 怎么用Java生成客户端调用接口
- Java 调用C# webservice接口 生成java客户端 实现方式
- 微信开放平台 公众号第三方平台开发 教程四 代公众号调用接口的SDK和demo
- iOS app支付宝接口调用的一点总结(补充支付宝SDK&Demo下载地址)
- 转:iOS app支付宝接口调用的一点总结(补充支付宝SDK&Demo下载地址)
- vue cli引入微信sdk,完成微信接口调用——引入微信sdk(一)
- 调用微信JS-SDK自定义分享以及其他接口
- php中xml数据接口如何生成及调用
- 生成webservice接口以及通过客户端调用接口。
- 利用JS-SDK微信分享接口调用(后端.NET)
- Oracle Form中调用并发请求生成报表并输出为PDF的方法 (FND_CONCURRENT.WAIT_FOR_REQUEST)
- thrift使用(2):代码生成和接口调用
- 怎么根据linux c/c++ 开发的d-bus服务生成java 的调用接口文件