Jump to content

Last day of the current month


brggabat

Recommended Posts

I copied the code from the previous post for the last day of the month but it didn't work. There is an error on the expression.

"new Date(System.currentTimeMillis() + 24*3600*1000*(new java.lang.Integer((new java.util.GregorianCalendar().getActualMaximum((java.util.GregorianCalendar.DAY_OF_MONTH))) - (new java.util.GregorianCalendar().get(java.util.GregorianCalendar.DATE)))).longValue())"

What would be the right code to get the last day of the month?

 

 

 

Link to comment
Share on other sites

  • Replies 15
  • Created
  • Last Reply

Top Posters In This Topic

If using Groovy in report design template (JRXML file), user can take advantage of Calendar.getActualMaximum method to calculate the last day of the month from user’s input date in JR through hidden parameters. It will take a few steps, however, to mutate the Calendar object using setter method in JR report design to accomplish this. Please refer to the following customer wiki posting to see how it can be done.

http://community.jaspersoft.com/wiki/how-get-last-day-month-input-control-report-design

Link to comment
Share on other sites

  • 1 year later...

This works to find the first day of this month:

new SimpleDateFormat("MM/dd/yyyy").format(java.util.Date.parse(MONTH(NOW( )) + "/1/" + YEAR(NOW())))[/code]

 

... and this will find the last day of this month:

new SimpleDateFormat("MM/dd/yyyy").format(java.util.Date.parse(MONTH(NOW( )) + "/" + DAYSINMONTH(NOW())+ "/" + YEAR(NOW())))[/code]

 

Link to comment
Share on other sites

  • 10 months later...
  • 4 months later...

Hi,

I have created some more of the queries gpayne_1 mentioned:

// First of month
new SimpleDateFormat("MM/dd/yyyy").format(java.util.Date.parse("01/" + MONTH(NOW( )) + "/" + YEAR(NOW())))

// First of previous month
new SimpleDateFormat("MM/dd/yyyy").format(java.util.Date.parse("01/" + (MONTH(NOW( )) - 1) + "/" + YEAR(NOW())))

// First of pre-previous month
new SimpleDateFormat("MM/dd/yyyy").format(java.util.Date.parse("01/" + (MONTH(NOW( )) - 2) + "/" + YEAR(NOW())))

// Last of month
new SimpleDateFormat("MM/dd/yyyy").format(java.util.Date.parse(MONTH(NOW( )) + "/" + DAYSINMONTH(NOW())+ "/" + YEAR(NOW())))

 

I am now struggling with getting the last day of the previous month. I tried something like below, but of course this does not work properly, as not all months end on the same day... 28 vs. 29. vs. 30 vs. 31.

// Last of previous month
new SimpleDateFormat("MM/dd/yyyy").format(java.util.Date.parse(
 (MONTH(NOW( )) - 1)
 + "/" +
 DAYSINMONTH(NOW())
 + "/" +
 YEAR(NOW())
))

Do you knwo of a way to retrieve the last (day) of the previous month?

 

Thanks in advance, greetings.

Link to comment
Share on other sites

JasperReports depends on commons-lang:2.6 through castor-xml:1.3.3 which means that you can use org.apache.commons.lang.time.DateUtils 

To use import DateUtils and Calender or speficy full package name in method call

<import value="java.util.Calendar"/><import value="org.apache.commons.lang.time.DateUtils"/>[/code]

Last day of the current month

DateUtils.addDays(DateUtils.truncate(DateUtils.addMonths($F{myField},1), Calendar.MONTH),-1)[/code]

Last day of previous month

DateUtils.addDays(DateUtils.truncate($F{myField}, Calendar.MONTH),-1)[/code]

For full example see How to display last day of previous month relative to a date?

Note: JasperStudio throws compilation error when using celing, currently I'm not sure why, maybe the version is older then the offical (method was not present in previous release), that's why I do not use this method.

Link to comment
Share on other sites

  • 1 month later...
  • 6 months later...

forgive me but the answers above will be fine for this year but if you want a date in the past year using 

YEAR(NOW()), will give the wrong answer.

its its June now, how do I get the date 6 months ago?

?

One answer is : use the apache commons lang time library

adding this to a jasper report was a difficult and awkward as the rest of jaspersoft and when you finally get it installed, guess what the expressions editor (which has a font that is too small to read) does not give you any intellisence on the classes/methods available, super helpful TIBCO NOT!

then you can add a negative amount of months to get desired result.

new SimpleDateFormat("MMM").format(DateUtils.addMonths(NOW(), -6))

 

Link to comment
Share on other sites

  • 2 years later...
  • 10 months later...
  • 3 months later...
  • 7 months 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...