Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
538 views
in Technique[技术] by (71.8m points)

java - How to pass Date as parameter to jasper report

I am trying to create JR report which is taking start_date and end_date as parameters.

The query:

SELECT * FROM emp WHERE joining_date BETWEEN $P{frm_date} AND $P{to_date}

The code:

Date from_date = dt_from_date.getDate();
Date to_date = dt_to_date.getDate();
java.sql.Date frm_dte = new java.sql.Date(from_date.getTime());
java.sql.Date to_dte = new java.sql.Date(to_date.getTime());
try {
    HashMap map = new HashMap();
    map.put("$P{frm_date}", frm_dte);
    map.put("$P{to_date}", to_dte);
    JasperPrint jp = JasperFillManager.fillReport(is, map, con);
    JRViewer jv = new JRViewer(jp);
    JFrame jf = new JFrame();
    jf.getContentPane().add(jv);
    jf.validate();
    jf.setVisible(true);
    jf.setSize(new Dimension(800, 600));
    jf.setLocation(300, 100);
    jf.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
} catch (JRException ex) {
    ex.printStackTrace();
}

Can we pass Two Parameters to same Column in the table? Eg:

map.put("joining_date", frm_dte); 
map.put("joining_date", to_dte);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can pass date as string format type as follow,

if(from_date!=null)
{
      formattedEndDate=new SimpleDateFormat("yyyy-MM-dd").format(from_date);
}

if(getStartDate()!=null)
{
    formattedStartDate=new SimpleDateFormat("yyyy-MM-dd").format(to_date);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...