Jump to content
  • year function


    nektarios.arakas
    Assigned User teodord
    CategoryBug report
    Component: 12896
    PriorityNormal
    ReproducibilityAlways
    ResolutionNo Change Required
    SeverityMajor
    StatusResolved

    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


    Attachments:

    User Feedback

    Recommended Comments

    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/jasperreports/ext/functions/src/main/java/net/sf/jasperreports/functions/standard/DateTimeFunctions.java#L624

     

    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 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!

    Link to comment
    Share on other sites

    Changed Status from New to Feedback Requested

    Changed Assigned User from - to @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.jasperreports.default.localeBut 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
    Link to comment
    Share on other sites



    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...