Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 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 Answer

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...