Jump to content

Formatting date in report


pvelan

Recommended Posts

  • Replies 6
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

I can tell you of _a_ way but it might not be the best.  What I would do is set a text field expression to use a date and then I would set the expression to something like:

new java.text.SimpleDateFormat(" ").parse(" ")

The first " " would be the pattern to create the date and the second " " would your string to create the date from.

----

Now, if you're using the XMLDataSource, though, skip all of what I just said and instead just set your parameter of XML_DATE_PATTERN to your pattern and you should be good.

From:

http://jasperreports.sourceforge.net/sample.reference/xmldatasource/index.html

 

 

Link to comment
Share on other sites

 Hello,

Thanks for your suggestions, but I am getting the following error...

Caused by: net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression : 
    Source text : new java.text.SimpleDateFormat("MM/dd/yyyy").parse($F{startDate}) 
    at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:195) 
    at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:589) 
    at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:557) 
    at net.sf.jasperreports.engine.fill.JRFillElement.evaluateExpression(JRFillElement.java:929) 
    at net.sf.jasperreports.engine.fill.JRFillTextField.evaluateText(JRFillTextField.java:383) 
    at net.sf.jasperreports.engine.fill.JRFillTextField.evaluate(JRFillTextField.java:368) 
    at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:258) 
    at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:499) 
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2036) 
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:760) 
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:270) 
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:128) 
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:946) 
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:864) 
    at net.sf.jasperreports.engine.fill.JRFillSubreport.fillSubreport(JRFillSubreport.java:613) 
    at net.sf.jasperreports.engine.fill.JRSubreportRunnable.run(JRSubreportRunnable.java:59) 
    at net.sf.jasperreports.engine.fill.JRThreadSubreportRunner.run(JRThreadSubreportRunner.java:205) 
    at java.lang.Thread.run(Thread.java:662) 
Caused by: java.text.ParseException: Unparseable date: "20111014 145300" 
    at java.text.DateFormat.parse(DateFormat.java:337) 

 

I tried substring option to just take "20111014". Even this gives the same error.

When i want to preview, how do I set "XML_DATE_PATTERN" in ireport?

Thanks

Link to comment
Share on other sites

If you are using the XML Data Source then please just follow the web link I posted above and set your XML_DATE_PATTERN paramater with a good default value and you should start getting dates returned when your xml is parsed.  Really, that's the best way to set the date pattern globally for your xml data source.

Now, if you have multiple types of date patterns for your xml then just setting XML_DATE_PATTERN isn't going to do you much good.  Your problem above is that you're trying to set the date format to what you want in your report.  You don't want to do that.  Instead, you need to set your date format to what you're given in your xml file for parsing into the java.lang.date.

I don't know what kind of date string you have above but if I wanted to parse from xml the date string of 20111014 which would be 2011, Oct, 14th then I would do this:

new java.text.SimpleDateFormat("yyyyMMdd").parse("20111014");

Once you have it parsing into a valid java.lang.date, then you can use IReport to define your date mask and be on your merry way.

 

 

 

Link to comment
Share on other sites

  • 3 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...