Im using the followin script/function (named getQuarter) to get a last day of current quarter:
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)/3 * 3 + 2);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
return cal.getTime();
And Im calling this function in my trigger which supposed to set this date every time when certain events occurs:
setAttribute('EffectiveDate', adf.util.getQuarter(today()))
But then the following error occurs when the trigger should start working:
Exception in expression "util" global function getQuarter2(java.util.Date) : groovy.lang.MissingMethodException : No signature of method: java.util.GregorianCalendar.set() is applicable for argument types: (java.lang.Integer, java.math.BigDecimal) values: [2, 5] Possible solutions: set(int, int), get(int), set(int, int, int), set(java.util.Map), next(), isSet(int)
I figured out it needs the type GregorianCalendar converted to Integer, but how Im supposed to do this in my script? Any ideas?
Alex
You can cast the values to int:
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.set(Calendar.MONTH as int, (cal.get(Calendar.MONTH)/3 * 3 + 2) as int);
cal.set(Calendar.DAY_OF_MONTH as int, (cal.getActualMaximum(Calendar.DAY_OF_MONTH)) as int);
If you're on Java 8, can I suggest you us the new java.time classes?
Given todays date:
def date = LocalDate.now()
You can calculate the last day of the quarter by:
int quarter = YearMonth.from(date).get(IsoFields.QUARTER_OF_YEAR)
LocalDate lastDayOfQuater = date.withMonth(quarter * 3).with(TemporalAdjusters.lastDayOfMonth())
Related
I want get the current date from the system(for eg- 2021-06-22) and compare with the input date (for eg- 2021-06-22) in groovy code.I tried the below code but im getting error in month field.
TimeZone tz = TimeZone.getTimeZone('UTC');
cal.setTimeZone(tz);
int hour = cal.get(Calendar.HOUR_OF_DAY) //Get the hour from timestamp
int min = cal.get(Calendar.MINUTE) //Get the Minutes from timestamp
int sec = cal.get(Calendar.SECOND)
int day = cal.get(Calendar.DAY_OF_WEEK)
int date = cal.get(Calendar.DATE)
int month= cal.get(Calendar.MONTH)
int year= cal.get(Calendar.YEAR)
println month
I am trying to create this block of code:
var nextWorkday time.Date
// var nextWorkday *time.Date // neither works
yyyy, mm, dd := now.Date()
goalTime, _ := time.ParseDuration(fmt.Sprintf("%fh", *goal))
goalSeconds := int(goalTime.Seconds())
if date.Weekday() != time.Friday { // wait till the next workday (7am + difference)
nextWorkday = time.Date(yyyy, mm, dd+1, 7, 0, 0+goalSeconds, 0, now.Location())
} else {
nextWorkday = time.Date(yyyy, mm, dd+3, 7, 0, 0+goalSeconds, 0, now.Location())
}
time.Sleep(nextWorkday)
The important breaking point is already the first line. I do not know how to declare a new variable of a custom type. Right now I get the error:
time.Date is not a type
What am I doing wrong? Any help appreciated!
There is no time.Date type in the standard time package. There is however a time.Time type which represents a time instant, "including" date.
time.Date() is a function which constructs a time.Time value from the provided date and time fields.
I am getting Date as '2/12/2020 4:30:29 PM'. But i need trimmed year and seconds date time format like
'12/02/20 04:30 PM'
what is the equivalent function in MEL for getting above date time?
Thanks
I'm afraid there isn't an equivalent function in MEL to do this thing. BUT the good news is that you can create it! (yes, MEL needs more time functions to work...).
I've create this global function that you can use it (I assume 2 is day and 12 is month, if not you can change the order):
separator = "/";
space = " ";
hourSeparator = ":";
$global:stringDatetimeToArray = function(datetime)
{
array['year'] = substring(datetime, 6, 10);
array['month'] = substring(datetime, 3, 5);
array['day'] = substring(datetime, 0, 2);
array['hour'] = substring(datetime, 11, 13);
array['minute'] = substring(datetime, 14, 16);
array['seconds'] = substring(datetime, 17, 19);
array['meridian'] = substring(datetime, 20, 22);
return array;
};
concat(array['month'], separator, array['day'], separator, array['year'], space, array['hour'], hourSeparator, array[minute], space, array['meridian']);
My recommendation for you is to generate a method that converts a timestamp into an array of these values, and then you can work with these kind of issues easier than now. You can see an example on this github script
import re
date_str = '2/12/2020 4:30:29 PM'
sub_str = re.search(':\d+(.*?)\s',date_str).group(1)
date_str = date_str.replace(sub_str,'')
print(date_str)
Output: 2/12/2020 4:30 PM
dcl-s today date inz(*Sys) is the current sytem date
How do i get the first day of the first month of the current year in this format 2017/01/01
You may want to look into the %SUBST built-in function.
date = d'1999-02-17';
time = t'01.23.45';
timestamp = z'1999-02-17-01.23.45.98765';
num = %subdt(date:*YEARS);
// num = 1999
num = %subdt(time:*MN);
// num = 23
Here's a slick trick:
dcl-proc BuildDate;
dcl-pi *n date;
pyear int(5) const;
pmonth int(5) const;
pday int(5) const;
end-pi;
dcl-ds *n;
dateds date(*iso) inz(d'0001-01-01');
year zoned(4:0) pos(1);
month zoned(2:0) pos(6);
day zoned(2:0) pos(9);
end-ds;
year = pyear;
month = pmonth;
day = pday;
test(e) dateds;
if %error;
reset dateds;
endif;
return dateds;
end-proc;
Now all you need to do to construct a valid date given Day Month and Year is:
FirstDOY = BuildDate(%subdt(%date(): *Y): 1: 1);
the following does not seem to work, it seems to cause an infinite loop:
import java.text.SimpleDateFormat;
SimpleDateFormat out=new SimpleDateFormat('yyyy-MM-dd');
def from = Calendar.instance
from.set(year: 2017, month: Calendar.JANUARY, date: 3)
def to = Calendar.instance
to.set(year: 2017, month: Calendar.FEBRUARY, date: 3)
from.upto(to) {
cal=it;
prev=cal;
prev.set(Calendar.DAY_OF_MONTH, 1);
println out.format(prev.getTime());
}
can somebody please explain why this should not work? I don't get it. My goal is to get the first day of month within the upto loop.
Inside the loop, you are constantly setting the calendar back to the first day of the month...
It's similar to if you did:
for (int i = 0; i < 10; i++) {
i = 0
println i
}
(that will never finish either)
Also, you code will run for every day between the two dates... which I don't think is what you are looking for either
It's easier if you use immutable things over Calendar, and as you're on Java 8, you can do:
import java.time.*
import java.time.format.*
// Add a next method, so you can do ranges of LocalDates
LocalDate.metaClass.next = { delegate.plusDays(1) }
LocalDate from = LocalDate.of(2017, 1, 3)
LocalDate to = LocalDate.of(2017, 2, 3)
(from..to).each {
println it.format(DateTimeFormatter.ISO_DATE) + " : " + it.withDayOfMonth(1).format(DateTimeFormatter.ISO_DATE)
}