on 2020 May 16 8:04 AM
Hi Experts,
I have a requirement to make an API call to get the invoices if they are created on that current month.
But, the created date in the invoice is in Unix timestamp.
My approach is as below:
* Convert the first day and last of the current month range to Unix Timestamp and store it in a property.
*Use that property in the API query to fetch. So this will be a dynamic API call for each run of the IFlow.
So, looking for groovy scripts or any better alternative approaches, If any.
Regards,
Pavan G
Hi Pavan
Your approach is sound. However, the devil is in the detail.
If the API requires two timestamps A and B, the range probably includes A, but does it exclude B? I.e. does it find timestamps A <= x < B or A <= x <= B? The former case is easier (and probably more likely), because B is then the timestamp of the first day of the next month.
Also, that leaves the question of actually finding those timestamps. Keep in mind that the server might not be (and probably isn't) in the same time zone as you. Therefore, make sure to explicitly state your time zone.
Here's some code using the Java 8 java.time package to find the timestamps:
ZonedDateTime midnightToday = ZonedDateTime.now(ZoneId.of("UTC+2")).truncatedTo(ChronoUnit.DAYS)
ZonedDateTime firstOfThisMonth = midnightToday.withDayOfMonth(1)
ZonedDateTime firstOfNextMonth = firstOfThisMonth.plusMonths(1)
long timestampLower = firstOfThisMonth.toEpochSecond()
long timestampUpper = firstOfNextMonth.toEpochSecond()
Remember to adjust the time zone! The UTC+2 one above is for Denmark 🙂 Also, the code requires the following imports:
import java.time.ZonedDateTime
import java.time.ZoneId
import java.time.temporal.ChronoUnit
Let me know how it works out.
Regards,
Morten
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Morten,
Thanks for the details:
I am facing issues with my Groovy Script { I am trying to convert it to Groovy Script from Java Script }
The output of fd gives me = 120-4-1 instead of 2020-04-1
Can you help me resolving this code ?
If this code works, I will use the same code for the last day of the previous month.
Then in API query, I will use FDPM >= X <=LDPM.
def Message processData(Message message) {
def firstDay = new Date();
//setting date to first day of previous Month
firstDay.setDate(1);
def month = firstDay.getMonth();
def day = firstDay.getDate();
def year = firstDay.getYear();
//generating first day of previous month
def fd = year + "-" + month + "-" + day;
//generating the first day of previous month with timezone
def tz = new Date(fd);
def fdpm=d.setTime(tz.getTime()/1000);
println month
//Set the first day of the previous months to Property
//message.setProperty("FDPM",fdpm);
}
Hi 7a519509aed84a2c9e6f627841825b5a,
I missed setting properties when I was using your code.
I corrected, it is working fine now.
Thanks a lot for providing me the code. It helped me a lot. 🙂
Regards,
Pavan
User | Count |
---|---|
71 | |
11 | |
11 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.