How to get start and end date for a specific month and year

前端 未结 3 1240
青春惊慌失措
青春惊慌失措 2021-01-24 06:26

I am creating a report in iReport

The user enters a date eg. 2014-09-14

The report then needs to print 2014-09-01 and 2014-09-30

(\"First day of

相关标签:
3条回答
  • 2021-01-24 06:56

    I got it working by using some SQL in the query of the report and just printing that field

    SQL looks like this

    Select LAST_DAY('2014-09-14') lastDay,
    DATE_ADD((DATE_ADD(LAST_DAY('2014-09-14') ,INTERVAL 1 DAY)) ,INTERVAL -1 MONTH) firstDay
    

    Note: replace '2014-09-14'with input parameter on reprot eg. $P{dateEntered}

    0 讨论(0)
  • 2021-01-24 07:13

    If you like to do this inside jasper report a good way to go is using variable

    Example: lets assume the date is arriving as a parameter (if you like naturally it can be a field, you just need to switch how the variable is calculated).

    The incoming date (I use directly the java.util.Calendar as class and set a default value so that I can test in preview)

    <parameter name="date" class="java.util.Calendar" isForPrompting="false">
        <defaultValueExpression><![CDATA[new GregorianCalendar()]]></defaultValueExpression>
    </parameter>
    

    The variable definition, this is beginning of month, create another variable for end of month using getActualMaximum instead of getActualMinimum

    <variable name="beginningOfMonth" class="java.util.Calendar">
        <initialValueExpression><![CDATA[new java.util.GregorianCalendar($P{date}.get(Calendar.YEAR),$P{date}.get(Calendar.MONTH) ,$P{date}.getActualMinimum(Calendar.DAY_OF_MONTH))]]></initialValueExpression>
    </variable>
    

    since we are using a parameter its enough to set initialValueExpression if you are using a field you need to set the variableExpression

    Thats it we have our date, format it as you like in the textField

    Example

    <textField>
      <reportElement x="22" y="11" width="100" height="20" uuid="1a094181-bcf1-469c-a786-77a0b7a3d533"/>
      <textFieldExpression><![CDATA[new java.text.SimpleDateFormat("yyyy-MM-dd").format($V{beginningOfMonth}.getTime())]]></textFieldExpression>
    </textField>
    

    Note: if you are using old version of jasper report (3.1 ecc) you need to set correct class on the different expression 's

    0 讨论(0)
  • 2021-01-24 07:14

    Try this out:

    Calendar abc = new GregorianCalendar();
    System.out.println(abc.getActualMaximum(Calendar.DAY_OF_MONTH) + " and " + abc.getActualMinimum(Calendar.DAY_OF_MONTH));
    
    0 讨论(0)
提交回复
热议问题