brggabat Posted September 5, 2014 Share Posted September 5, 2014 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 More sharing options...
rajesh.sirsikar Posted September 8, 2014 Share Posted September 8, 2014 Hi brggabat,Simplest way to achieve it is by creating two variables.Variable 1 will have the expression to find the current date : 2) Another Variable with Class as Integer with expressionRajeshwww.rajeshsirsikar.com Link to comment Share on other sites More sharing options...
Tom C Posted September 8, 2014 Share Posted September 8, 2014 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 More sharing options...
gpayne_1 Posted June 28, 2016 Share Posted June 28, 2016 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 More sharing options...
djohnson53 Posted June 28, 2016 Share Posted June 28, 2016 What was the previous post that had it wrong? Link to comment Share on other sites More sharing options...
cheapwebhoster Posted May 25, 2017 Share Posted May 25, 2017 gpayne_1 solution works perfectly for a field data but how to apply this inside input control parameter? what is the class type? calendar or java.sql.date or string? Link to comment Share on other sites More sharing options...
cheapwebhoster Posted May 25, 2017 Share Posted May 25, 2017 We need to convert the "/" from the date format into "-" so that it looks like 2017-05-01 then this will works... Link to comment Share on other sites More sharing options...
54gfcxhbgfh554 Posted September 25, 2017 Share Posted September 25, 2017 Hi,I have created some more of the queries gpayne_1 mentioned:// First of monthnew SimpleDateFormat("MM/dd/yyyy").format(java.util.Date.parse("01/" + MONTH(NOW( )) + "/" + YEAR(NOW())))// First of previous monthnew SimpleDateFormat("MM/dd/yyyy").format(java.util.Date.parse("01/" + (MONTH(NOW( )) - 1) + "/" + YEAR(NOW())))// First of pre-previous monthnew SimpleDateFormat("MM/dd/yyyy").format(java.util.Date.parse("01/" + (MONTH(NOW( )) - 2) + "/" + YEAR(NOW())))// Last of monthnew 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 monthnew 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 More sharing options...
petter.friberg Posted September 27, 2017 Share Posted September 27, 2017 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 monthDateUtils.addDays(DateUtils.truncate(DateUtils.addMonths($F{myField},1), Calendar.MONTH),-1)[/code]Last day of previous monthDateUtils.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 More sharing options...
teresco Posted November 22, 2017 Share Posted November 22, 2017 Was also in desperate search for autosetting 1st and last days of last month, when found this:https://community.jaspersoft.com/wiki/how-use-relative-dates-jrxml-static-reportsSimple and working as intended. Link to comment Share on other sites More sharing options...
jasperjones999 Posted June 12, 2018 Share Posted June 12, 2018 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 libraryadding 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 More sharing options...
mjmapi Posted February 24, 2021 Share Posted February 24, 2021 I use new SimpleDateFormat("dd/MM/yyyy"+" "+"00:00:00").format(java.util.Date.parse(MONTH(NOW( )) + "/" + DAYSINMONTH(NOW())+ "/" + YEAR(NOW()))) Link to comment Share on other sites More sharing options...
gorza Posted January 17, 2022 Share Posted January 17, 2022 //Last day of the current monthjava.sql.Date.valueOf( java.time.LocalDate.now().withDayOfMonth(1).plusMonths(1).minusDays(1) )[/code] Link to comment Share on other sites More sharing options...
ahmed.m.k.h.s Posted April 25, 2022 Share Posted April 25, 2022 you can get the first day of the month using the following expression:DATE(YEAR($F{ADATE}),MONTH($F{ADATE}),1)you can get the last day of the month using the following exprssion:DATE(YEAR($F{ADATE}),MONTH($F{ADATE}),DAYSINMONTH($F{ADATE})) Link to comment Share on other sites More sharing options...
cooperTWICE Posted December 8, 2022 Share Posted December 8, 2022 when I am using dockerfile 8.1.0-debian-11-r29.DAYSINMONTH() this function is not working at all.Is there anyone have the same issue? Link to comment Share on other sites More sharing options...
lucianc Posted December 8, 2022 Share Posted December 8, 2022 @cooperTWICE, see https://github.com/TIBCOSoftware/jasperreports/issues/286 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now