danoldenkampgmail.com Posted May 10, 2019 Share Posted May 10, 2019 First day of this month:new SimpleDateFormat("yyyy-MM-dd").format(java.util.Date.parse(MONTH(NOW( )) + "/1/" + YEAR(NOW())))[/code]Last day of this month:new SimpleDateFormat("yyyy-MM-dd").format(java.util.Date.parse(MONTH(NOW( )) + "/" + DAYSINMONTH(NOW())+"/" + YEAR(NOW())))[/code]First day of last month:new SimpleDateFormat("yyyy-MM-dd").format(java.util.Date.parse((MONTH(NOW( )) -1 ) + "/1/" + YEAR(NOW())) )[/code]NOT WORKING:Last Day of Last month: set the days to DaysInMonth (LAST MONTH FIRST OF MONTH) new SimpleDateFormat("yyyy-MM-dd").format(java.util.Date.parse((MONTH(NOW( )) -1 ) + "/" + DAYSINMONTH(MONTH(NOW( )) -1 ) + "/1/" + YEAR(NOW())) + "/" + YEAR(NOW()))[/code]I've tried this a dozen different ways, but just can't get the Last Day of Last Month to work.(Common question as many business users run a report for the last month.)Can anyone more experienced than I with this shed some light?(I could do this easy with SQL, Maybe I could set the default expression from a seperate SQL query field? Link to comment Share on other sites More sharing options...
C-Box Posted May 13, 2019 Share Posted May 13, 2019 Hi,I'd advice using the also in JasperReports used Apache DateUtils.... the commons-lang3.jar should be available in your classpath of course .... so take a look at this:DateUtils.addSeconds(DateUtils.ceiling(DateUtils.addMonths(new Date(), -1), Calendar.MONTH), -1)[/code]It will calculate the last Day of the previous month from Today (better: the latest second in the previous month :-) ).So an sysout will show you, if you start it today (13.05.2019):Tue Apr 30 23:59:59 CEST 2019hth + regardsC-Box Link to comment Share on other sites More sharing options...
danoldenkampgmail.com Posted May 17, 2019 Author Share Posted May 17, 2019 Thanks for the suggestion. I'm using out of box Jaspersoft Studio.I don't think I have access to that JAR. I tried using that for my Paramater default value expression (Class: Java.util.Date) :Error evaluating expression for source text: DateUtils.addSeconds(DateUtils.ceiling(DateUtils.addMonths(new Date(), -1), Calendar.MONTH), -1)com.jaspersoft.jasperserver.api.JSExceptionWrapper: net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression for source text: DateUtils.addSeconds(DateUtils.ceiling(DateUtils.addMonths(new Date(), -1), Calendar.MONTH), -1) Link to comment Share on other sites More sharing options...
C-Box Posted May 20, 2019 Share Posted May 20, 2019 Hi again,just tried with JasperSoftStudio 6.6.0 ... also out of the box and it works well. The required library is also included within that JRStudio version:e.g. "C:JasperJaspersoft Studio-6.6.0pluginsorg.apache.commons.lang3_3.1.0.v201403281430.jar" Here is my design - just take a look at the imports at the beginning. Probably you should define the "JasperReports Dependencies" to your design-workspace classpath as well.<?xml version="1.0" encoding="UTF-8"?><!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.5.1 --><jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="TestLastDayOfPrevMonth" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="5cf53290-7072-4385-835a-aba7cba4db1c"> <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/> <import value="org.apache.commons.lang3.time.*"/> <parameter name="ParamLastDayCalced" class="java.util.Date" isForPrompting="false"> <defaultValueExpression><![CDATA[DateUtils.addSeconds(DateUtils.ceiling(DateUtils.addMonths(new Date(), -1), Calendar.MONTH), -1)]]></defaultValueExpression> </parameter> <queryString> <![CDATA[]]> </queryString> <background> <band splitType="Stretch"/> </background> <title> <band height="79" splitType="Stretch"> <textField> <reportElement x="230" y="41" width="281" height="30" uuid="2b9e697e-3d75-4fc7-a96d-41e7901c474c"/> <textFieldExpression><![CDATA[$P{ParamLastDayCalced}]]></textFieldExpression> </textField> <staticText> <reportElement x="10" y="41" width="217" height="30" uuid="24c36840-3b6c-4eac-a79b-71a4db495427"/> <text><![CDATA[Last Day of Last month:]]></text> </staticText> <staticText> <reportElement x="10" y="10" width="217" height="30" uuid="68d423b1-c47c-4c53-a736-130cd8641d5c"/> <text><![CDATA[Today:]]></text> </staticText> <textField> <reportElement x="230" y="11" width="281" height="30" uuid="090cdc6e-d2e5-4848-826d-d91ac909763d"/> <textFieldExpression><![CDATA[new Date()]]></textFieldExpression> </textField> </band> </title> <pageHeader> <band height="35" splitType="Stretch"/> </pageHeader> <columnHeader> <band height="61" splitType="Stretch"/> </columnHeader> <detail> <band height="125" splitType="Stretch"/> </detail> <columnFooter> <band height="45" splitType="Stretch"/> </columnFooter> <pageFooter> <band height="54" splitType="Stretch"/> </pageFooter> <summary> <band height="42" splitType="Stretch"/> </summary></jasperReport>[/code]so works as expected.hth + regardsC-Box Link to comment Share on other sites More sharing options...
danoldenkampgmail.com Posted May 23, 2019 Author Share Posted May 23, 2019 Thanks C-Box.Importing the library for DateUtil,report > Imports > org.apache.commons.lang3.time.*;Parameter default expression: DateUtils.addSeconds(DateUtils.ceiling(DateUtils.addMonths(new Date(), -1), Calendar.MONTH), -1)Worked great. You the man now, dog! Thank you! Link to comment Share on other sites More sharing options...
C-Box Posted May 23, 2019 Share Posted May 23, 2019 No problem! Thanks for your feedbackregards from Dresden/Germany and goof luck with "Jaspering" C-Box Link to comment Share on other sites More sharing options...
sam.sabbaghpromontech.com Posted October 8, 2019 Share Posted October 8, 2019 This works for finding last day of last month and is super simple:Today(0 - DayNumber(Today())) Link to comment Share on other sites More sharing options...
tony.dunlop Posted March 4, 2020 Share Posted March 4, 2020 this seems to workNOTE both of the following require the local date class to be added to the reporteither add this to the <jasperReport> section at the top of source file of the report next to the <property> values :<import value="java.time.LocalDate"/>[/code]or use jasper studio and click report->properties->import->add class then type localDate, see image attached First day of last month default date java.sql.Date.valueOf(LocalDate.now().withDayOfMonth(1).minusMonths(1))[/code]Last day of last month java.sql.Date.valueOf(LocalDate.now().withDayOfMonth(1).minusDays(1))[/code] Link to comment Share on other sites More sharing options...
mjmapi Posted June 29, 2021 Share Posted June 29, 2021 /sites/all/libraries/ckeditor/plugins/smiley/images/thumbs_up.png Link to comment Share on other sites More sharing options...
mjmapi Posted June 29, 2021 Share Posted June 29, 2021 I use the followingFirst Day of Last Monthnew SimpleDateFormat("yyyy/MM/dd"+" "+"00:00:00").format(java.util.Date.parse("01/" + (MONTH(NOW( )) - 1) + "/" + YEAR(NOW())))Last day of last monthnew SimpleDateFormat("yyyy/MM/dd"+" "+"23:59:59").format(java.util.Date.parse((MONTH(NOW( )) - 1) + "/" + DAYSINMONTH(new SimpleDateFormat("yyyy/MM/dd"+" "+"00:00:00").format(java.util.Date.parse("01/" + (MONTH(NOW( )) - 1) + "/" + YEAR(NOW()))))+ "/" + YEAR(NOW()))) Link to comment Share on other sites More sharing options...
C-Box Posted June 29, 2021 Share Posted June 29, 2021 Have you tried this also when you simulate being in January? So I doubt, that just subtracting 1 from current month will automatically switch to december and then also decrements the year to the past one!?!? Just give it a try and post your result.... would be interessting. regards from rainy Dresden/GermanyC-Box 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