根据输入日期通过位移方式解决包含当月的前几月自定义函数 http://reportsoft.blog.hexun.com/75077561_d.html
2012-04-25 09:58
477 查看
字号:大
中
小
1. 需求概述
A.报表里通常会用到同期比,上年同期比。对有些零售行业的报表可能要求比较高一些,对包含当月的前3,6,12个月的一些同期比,比上年同期等,因此数据的处理和计算起来常规的同期比就更复杂一些。由于会根据当前输入的日期去定位前3,6,12个月的范围, 润乾报表内置的lastmonth只能解决上个月,多个月则需要多层嵌套,这似乎不是较好的实现方法。
B.通过业务逻辑分析,可以得出通过自定义函数利用位移的方式来满足这种数据计算应该会比较方便。
C.对当前月,前3,6,12月范围内的数据进行统计,去年前3,6,12月范围内的数据进行统计,和当前年的进行上年同期比,如图:
整个报表设计预览效果图如下:
样例左半部分:
样例右半部分:
2. 算法实现及调用规则
A.思路:
1.根据当前输入的日期,通过JAVA内置日期函数进行调整,找到要返回前几月的月初,和月末,精确到时分秒.
2.用法:monthBefore(@arg1, -3),monthAfter(@arg1, -3)
如输入arg1为: 2010-09-04,返回结果为:
2010-06-01 00:00:00
2010-06-30 23:59:59
支持正负位移,-3代表包含当月的前3月的条件,前多少月以此类推,通过位移的方式实现比较方便.避免多层嵌套的麻烦.
B:报表设计参数模板上的用法:
自定义函数的登记:
3. 程序说明
A.调用格式:monthAfter(@arg1, -3),返回前几月的月末,精确到时分秒.
package expression;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import com.runqian.base4.util.ReportError;
import com.runqian.report4.model.expression.Expression;
import com.runqian.report4.model.expression.Function;
import com.runqian.report4.model.expression.Variant2;
import com.runqian.report4.usermodel.Context;
public class monthAfter extends Function {
/**
* 解决前3月,6月,12月,月末问题
*/
public Object calculate(Context ctx, boolean inputValue) {
if (this.paramList.size() == 0)
throw new ReportError("该函数需要两个参数!");
// 取得计算表达式
Expression exp1 = (Expression) this.paramList.get(0);
if (exp1 == null)
throw new ReportError("包含有无效参数!");
Object obj1 = Variant2.getValue(exp1.calculate(ctx, inputValue),
false,
false);
Expression exp2 = (Expression) this.paramList.get(1);
if (exp2 == null)
throw new ReportError("包含有无效参数!");
Object obj2 = Variant2.getValue(exp2.calculate(ctx, inputValue),
false,
false);
return get3MonthAfter(String.valueOf(obj1), Integer.valueOf(String
.valueOf(obj2)));
}
public static String get3MonthAfter(String ri, int ts) {
String monthEnd = "";
try {
Calendar cal = Calendar.getInstance();
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
Date d1 = s.parse(ri);
cal.setTime(d1);
cal.add(Calendar.MONTH, ts);
cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
Date d2 = cal.getTime();
monthEnd = s.format(d2) + " 23:59:59";// 月末
System.out.println(monthEnd);
} catch (Exception e) {
e.printStackTrace();
}
return monthEnd;
相关文章推荐
- 根据输入日期通过位移方式解决包含当月的前几月自定义函数
- 根据输入日期通过位移方式解决包含当月的前几月自定义函数
- 根据输入日期通过位移方式解决包含当月的前几月自定义函数
- 根据输入日期通过位移方式解决包含当月的前几月自定义函数
- 根据输入日期通过位移方式解决包含当月的前几月自定义函数
- 根据输入日期通过位移方式解决包含当月的前几月自定义函数
- http://yanghengjun.blog.hexun.com/26226494_d.html
- 改变图标!用户自定义列表样式css说明(http://www.blogcup.com/read_dicky_5345.html)
- making of success(http://potato.blog.hexun.com/3690350_d.html)
- SqlServer时间日期处理函数及字符串转换@http://blog.sina.com.cn/s/blog_634c33eb0101859v.html
- Java的基本I/O(输入/输出)系统copy from (http://blog.sina.com.cn/s/blog_5ec67df20100d6ao.html)
- php curl_init函数用法(http://blog.sina.com.cn/s/blog_640738130100tsig.html)
- Referenced file contains errors 解决方法 (转载地址:http://blog.sina.com.cn/s/blog_6a6b141001011xhn.html)
- C# 调用C++的dll,通过DllImport方式。 from http://www.cnblogs.com/xiaokang088/archive/2011/04/08/2009673.html
- C++ 输入输出流和文件流 (2010-01-17 13:34:03)http://blog.sina.com.cn/s/blog_700a65cc0100mi5o.html
- include和merge标记的作用主要是为了解决layout的重用问题。[轉 from:http://blog.sina.com.cn/s/blog_67d95f400100zk3d.html]
- Java的基本I/O(输入/输出)系统copy from (http://blog.sina.com.cn/s/blog_5ec67df20100d6ao.html)
- 决策树的剪枝理论 此博文包含图片 http://blog.sina.com.cn/s/blog_4e4dec6c0101fdz6.html
- http://lipingke.blog.hexun.com.tw/39367544_d.html
- Oracel DUAL 表的日常使用(http://blog.sina.com.cn/s/blog_53aed4430100axiv.html),更新session日期格式