Add months to variable date in Groovy - groovy

I'm looking to calculate 6 months from a variable date. I have list of names and the date they last logged in. In groovy, how do I add 6 months to any given date?
the variable containing dates is lastloggedin. It is of date type and in format yyyy-MM-dd
import groovy.time.TimeCategory
use (TimeCategory) {
sixmonthsfromnow = lastloggedin + 6.month
}
Example result:
if lastloggedin = 2021-01-02, the result = "2021-01-026month"

You can parse the string into LocalDate
def someDate = LocalDate.parse('2021-01-02', 'yyyy-MM-dd')
Or if you have the Date type, you can convert it into LocalDate and then add months
date.toLocalDate() + 6.month
And then add months to it
someDate + 6.month
Or by using TimeCategory
def format = "yyyy-MM-dd"
def date = new Date()
def formattedDate = today.format(format)
use(TimeCategory) {
def oneYear = today + 6.month
}

Related

extract last day and first day from month in python

i am new in python ,i have a string of this format
Ex. 'Mar-00'
how could i calculate first day and last day from this month.
OUTPUT :
first day : 01-Mar-00
last day : 31-Mar-00
ex : 'Feb-17'
first day : 01-Feb-17
last day : 28-Feb-17
you can use calendar.monthrange(year, month)[1] to get the number of days in a given month+year.
the first is alway 01, and last is the value of calendar.monthrange(year, month)[1]
try this, let me know if it works:
import calendar
month_abbr_to_num = {v: k for k,v in enumerate(calendar.month_abbr)}
month_abbr, year_2_digit = input("please enter month, e.g. Mar-00: ").split('-')
month = month_abbr_to_num[month_abbr]
year = int('20'+year_2_digit)
days_in_month = calendar.monthrange(year,month)[1]
print("first day : {:02d}-{}-{}".format(1, month_abbr, year_2_digit))
print("last day : {:02d}-{}-{}".format(days_in_month, month_abbr, year_2_digit))
EDIT:
following your comment on original question, here it is in the form of a function that returns both as datetime objects:
import calendar
import datetime
month_abbr_to_num = {v: k for k,v in enumerate(calendar.month_abbr)}
def get_first_and_last_day(user_input):
month_abbr, year_2_digit = user_input.split('-')
month = month_abbr_to_num[month_abbr]
year = int('20' + year_2_digit)
days_in_month = calendar.monthrange(year, month)[1]
first_day = datetime.datetime(year, month, 1)
last_day = datetime.datetime(year, month, days_in_month)
return first_day, last_day
first, last = get_first_and_last_day(input("please enter month, e.g. Mar-00: "))
print("first day : ",first)
print("last day : ",last)

How to get day from date using groovy

I want to get day (Ex Sun, Mon, Tue, ..) from date
def date = new Date()
def day = date[Calendar.DAY_OF_MONTH]
result is 18, but i want to get Mon.
And if I have 2017-10-09T00:00:00.000, I want to get day of 2017-10-09T00:00:00.000
Could you help me please?
The below code can help. It gets today's date first which is 18 Sep 2017 and then thats passed into calendar instance
Calendar instance then get the day in integer form i.e. monday=2, Then once we get the value 2 using map we say its Monday
def date = new Date()
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
def day = calendar.get(Calendar.DAY_OF_WEEK);
def map=[
1:"Sunday",
2:"Monday",
3:"Tuesday",
4:"Wednesday",
5:"Thursday",
6:"Friday",
7:"Saturday"]
log.info map[day]
Try Calendar.DAY_OF_WEEK. This returns an int which you can later map to the respective string.
Try using SimpleDateFormat, with EE format which tells days of week.
try below code :
def date = new Date()
SimpleDateFormat("EE").format(date) // EE means days of week
You can use this to output a three character representation of the name of the week day:
theDate = "2017-10-09T00:00:00.000";
def parsedDate = new Date().parse("yyyy-MM-dd'T'HH:mm:ss.SSS", theDate);
def day = new java.text.SimpleDateFormat("EEE", Locale.ENGLISH).format(parsedDate);
or for the full name use EEEE pattern.
Make sure to specify the Locale as this could differ per server configuration.

Python 3 working with date objects

I have a variable storing a date object
CreateDate = keyData["CreateDate"] that is storing a value like "2017-01-11 19:40:26+00:00" (depends on the date) and I am wanting to use that date in addition with today's date to find out how many days it has been between the two. So for example if the create date was 2017-07-10 and today's date is 2017-07-18 then I would like the output to be the amount of days passed. I have tried working with the datetime module like datetime.combine(date.today(), createdate) - datetime.combine(date.today(), todaysDate) but still run into issues and think there might be a simpler way to get the total days passed between the two.
todaysDate = strftime("%Y-%m-%d %H:%M:%S", gmtime())
todaysDate = str(todaysDate)
todaysDate = todaysDate[0:10]
todaysDate = datetime.strptime(todaysDate, "%Y-%m-%d")
CreateDate = datetime.strptime(CreateDate, "%Y-%m-%d")
totalDays = abs((CreateDate - todaysDate).days)
print (totalDays)
`

How to compare date in Groovy?

I need to compare two data in format 13.07.2017 14:03:51,469000000 using groovy
I try to do this, but get error message.
I get next data:
time1 = 13.07.2017 14:03:51,469000000
time2 = 13.07.2017 14:03:52,069000000
Then I try to compare it:
time1 = time1['TIME'] as String
time2 = time2['TIME'] as String
assert time1 > time2, 'Error'
Which type of value should I choose for date for compare it?
Whats wrong in my comparing?
You need to convert the string to Date and then compare as shown below.
In order to convert, the right date format should be used.
Here you go, comments inline:
//Define the date format as per your input
def df = "dd.MM.yyyy HH:mm:ss,S"
//Parse the date string with above date format
def dateTime1 = new Date().parse(df, "13.07.2017 14:03:51,469000000")
def dateTime2 = new Date().parse(df, "13.07.2017 14:03:52,469000000")
//Compare both date times
assert dateTime1 < dateTime2

Parse String to date, but result is different format

I have been trying to parse a string into dates, but after parsing, the format changes. I have this in my controller:
def dates = params.id
def dates2 = new Date().parse("yyyyy:MM:dd", dates)
System.out.println(dates2)
But the result is Tue Oct 23 00:00:00 CST 2012 instead of the the format that I added. How can I force it to that format yyyy:MM:dd?
That's just the String representation of a Java Date
If you use Date.format to print it out, you'll see the format you want:
def dates = params.id
// parse is static, no need for new
def dates2 = Date.parse( 'yyyy:MM:dd', dates )
println( dates2.format( 'yyyy:MM:dd' ) )
(I also assume you mean yyyy, not 5 ys)

Resources