[#11161] - year function

Category:
Bug report
Priority:
Normal
Status:
Feedback Requested
Project: Severity:
Major
Resolution:
Open
Component: Reproducibility:
Always
Assigned to:
0

I have a STRING field that contains a date in format "dd/MM/yy". When passing that value in YEAR function is not always correct. Sometimes is +1, other times is +2.

Example
07/08/53 gives 53
22/09/53 gives 54
29/08/54 gives 56.

I cant find a pattern.

Of course the field should have been date and using SimpleDateFormat and then YEAR works fine

AttachmentSize
Image icon capture.png20.84 KB
Functions
nektarios.arakas's picture
Joined: Apr 15 2015 - 11:05am
Last seen: 2 months 1 week ago

2 Comments:

#1

When converting a date String to a Date object in those built-in functions like YEAR, it looks like it tries the DateFormats from your Locale in the following order:
DateFormat.MEDIUM
DateFormat.SHORT
DateFormat.LONG
DateFormat.FULL

and uses the first one that it's successfully able to parse the String with.

JasperReports code: https://github.com/TIBCOSoftware/jasperreports/blob/jr-6-7-0/jasperrepor...

More info about the DateFormat class here: https://docs.oracle.com/javase/8/docs/api/java/text/DateFormat.html

For example, in my locale the SHORT DateFormat pattern is M/d/yy so it looks like it is treating the first number as the 'month' and thus appears to be rolling over into the next years.

e.g.

List<String> dateStrings = new ArrayList<>();
dateStrings.add("07/08/53");
dateStrings.add("22/09/53");
dateStrings.add("29/08/54");
DateFormat dfShort = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
System.out.println("DateFormat SHORT: " + ((SimpleDateFormat)dfShort).toLocalizedPattern());
for (String dateString : dateStrings) {
System.out.println("Converting date string " + dateString + ": " + dfShort.parse(dateString).toString());
}

Produces the following output for me:

DateFormat SHORT: M/d/yy
Converting date string 07/08/53: Wed Jul 08 00:00:00 CDT 1953
Converting date string 22/09/53: Sat Oct 09 00:00:00 CST 1954
Converting date string 29/08/54: Tue May 08 00:00:00 CDT 1956

Hope that helps!

#2
  • Status:New» Feedback Requested
  • Assigned:nobody» teodord

Hi,

Indeed, Clayton's assessment is correct.
It might help you if you would control the report locale using either the REPORT_LOCALE built-in parameter or the default locale configuration property:
http://jasperreports.sourceforge.net/config.reference.html#net.sf.jasper...

But I suggest you control the parsing of your dates using a SimpleDateFormat object as you showed us in the capture.
You can create that format object only once, using the defaultValueExpression of a dummy report parameter, and reuse it in all expressions.

I hope this helps.
Teodor

Feedback
randomness