![]() You could also get other useful information like the first day of the month (not very helpful because it is always 1), but you can get the last day of the month. So adding months is significantly easier with the "add" function and specifying the field, just that you would need to first create a Calendar object. keeping in tradition with the default date format Println (cal.format("EEE MMM dd hh:mm:ss z yyyy")) GregorianCalendar cal = new GregorianCalendar()Ĭal.setTime(new Date().parse("yyyy-MMM-dd", "2022-Dec-05")) More specifically the GregorianCalendar class (as Calendar is an abstract class). If you need to add months or years, then you would need the Calendar class. The date class is limited in its ability to do other types of arithmetic than taking differences in dates, or intervals, or adding/subtracting days. Äef dayDifference = (dateMinus. ![]() Additions and subtractions to arrive at other dates are really easy and straightforward. The main advantage of the Groovy date extension is doing arithmetic operations on date objects. There's a comprehensive list of subscript operators but probably most won't see common use. So remember to +1 when you need it to be month number There is an alternate way to get a portion of a date println (dateParse) Mostly you'll be using simple format options to convert the Date object to the relevant Year member name or Period member name. You can use the "format" function to format the Date object. Sometimes, you'll need to format the Date object into a desired format. Remember you can use drop(2) to drop the first two characters in the year string thus getting rid of "FY" and then use it in your string format. String yearCurrent = "FY22"Äate dateParse = new Date().parse("yy-MMM-dd", "$-01") One common situation you'll come across is when you're parsing the date from Year / Month dimension member names. "yyyy", "yy", "mm", "MMM", "d", and "dd" are the most common formatting patterns you'll need. Here's a complete list of the formatting patterns You can type in the date as the second argument, or pass a variable that refers to a date in a String format. The first part of the string defines the format in which we expect the date to be. Parsing Dates Date parseDate = new Date().parse("yyyy-MMM-dd", "2022-Dec-09") Println date // output: Sat Jan 15 00:00:) You can also initialize the date when creating the object by passing in the year, month, and day as arguments. That's it, you now have a Date object initialized to the current time. Creating Dates Date newDateObj = new Date() To let your creativity take full advantage, let's see what we can do with the Date class. pro-rating a new hire's salary using the date of hire and calculating the precise number of working days for the first month). With Groovy, using the Date class allows you to deliver functionality that you wouldn't have been able to do so with Essbase scripts (e.g. The Date object in Groovy provides a simple and intuitive way to work with dates and times in your code. One of the most useful features of Groovy is the built-in support for working with dates and times. Groovy is a powerful and versatile programming language that offers a wide range of features for developers. Usually, we limit ourselves to the Year and Month, and assume the calculation logic for the entire month and call it a day. Date Functions in Groovy for EPM Planning ScriptsÄate functions are the hardest to manipulate in Essbase scripts. ("Is 6 months older? " + isOlderThanMonths(date3, 6)) ![]() ("Is 6 months older? " + isOlderThanMonths(date2, 6)) LocalDate date2 = LocalDate.of(2020, 9, 26) ("Is 6 months older? " + isOlderThanMonths(date1, 6)) LocalDate date1 = LocalDate.of(2020, 9, 25) We can use the ChronoUnit.between methods:Ĭ(Temporal t1, Temporal t2)Ĭ(Temporal t1, Temporal t2)Ĭ(Temporal t1, Temporal t2)Ĭ(Temporal t1, Temporal t2)Ĭ(Temporal t1, Temporal t2)Ĭ(Temporal t1, Temporal t2)ĬhronoUnit.HALF_DAYS.between(Temporal t1, Temporal t2)Ĭ(Temporal t1, Temporal t2)Ĭ(Temporal t1, Temporal t2)Ĭ(Temporal t1, Temporal t2)Ĭ(Temporal t1, Temporal t2)Ĭ(Temporal t1, Temporal t2)Ĭ(Temporal t1, Temporal t2)Ĭ(Temporal t1, Temporal t2)Ĭ(Temporal t1, Temporal t2)Ä¢.2 This example uses to check if a date is 6 months older than the current date. Majority new Java 8 date and time APIs are implemented the Temporal interface, for examples, LocalDate, LocalDateTime, ZonedDateTime, etc.
0 Comments
Leave a Reply. |