3/17/2023 0 Comments Date time calc![]() ![]() Some of you old Groovy users may be wondering why I’m not using good old groovy.Date. I could have also used Java Streams rather than Groovy ranges, lists, and closures. Note that I could have used the each closure instead of inject, or even a for loop. Next, I define the start and end dates between which I want to calculate working days.įinally, I call println using a Groovy GString to evaluate the calcWorkingDays() method and display the result. Returns the value of the result returned by inject().As long as d is neither in the holidaySet nor in the weekendDaySet, increments the subtotal by 1.Defines a range between start and end, open on the end, (that’s what Then I define a method calcWorkingDays() that takes as arguments the start date, the end date (which following the previous example of between() is the first value outside the range I want to consider), the holiday set, and the weekend day set. Next, I want to skip Saturdays and Sundays, so I create another set incorporating two enum values of – SATURDAY and SUNDAY. ![]() Note as well that I’m using the Groovy shorthand to create a List and then coercing it to a Set. Note that the default pattern for LocalDate.parse() is ‘ yyyy-MM-dd’, so I’ve left the pattern out here. Int calcWorkingDays (start, end, holidaySet, weekendDaySet ) working day(s) between $start and $end"Ĭopy this code into a file called wdb.groovy and run it from the command line to see the results:Ģ2 working day (s ) between -01įirst, I create a set of holiday dates (these are Chile’s “días feriados” for 2021, in case you wondered) called holidaySet. This handy script will calculate that for me:ĭef holidaySet = [LocalDate. More date arithmeticĮvery so often, I need to know how many working days are in a specific time frame (like, say, a month). Note that I used January 1, 2022, as the value for end this is because between() spans the time period starting on the first date given up to but not including the second date given. The class, actually an Enum, provides several Enum constants, like WEEKS (or DAYS, or CENTURIES.) which in turn provide the between() method that allows us to calculate the interval of those units between two LocalDates (or other similar date or time data types). Notice as well that in Java, parse() requires an instance of DateTimeFormat: parse (CharSequence text, DateTimeFormatter formatter )Īs a result, parsing becomes a two-step operation, whereas Groovy provides an additional version of parse() that accepts the format string directly in place of the DateTimeFormat instance. Notice that M represents “month,” not m, which represents “minute.” So this pattern defines a date formatted as a four-digit year, followed by a hyphen, followed by a two-digit month number (1-12), followed by another hyphen, followed by a two-digit day-of-month number (1-31). The format characters are explained in quite a number of places–for example, the documentation for. The class provides many useful static methods (like parse() shown above, which lets us convert from a string to a LocalDate instance according to a pattern, in this case, ‘yyyy-MM-dd’). Since Java 8, time and date calculations have been folded into a new package called java.time, and Groovy provides access to that. For this article, I'm using my distro's OpenJDK11 release and SDKMan's latest Groovy release. ![]() Alternately, you can install Groovy by following the instructions on the .Ī nice alternative for Linux users is SDKMan, which can be used to get multiple versions of Java, Groovy, and many other related tools. Both a recent and decent version of Java and Groovy might be in your Linux distribution's repositories. Groovy is based on Java, so it requires a Java installation. This is the perfect sort of problem to solve with a tiny Groovy script. So, yeah, I had to figure out how many weeks between -31 inclusive.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |