Incrementing a date in Groovy gets an incorrect date - groovy

In my Grails project I have a date in the controller and I need to increment this date by one month, so I did as below:
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD HH:MM:ss");
def temp
use (TimeCategory)
{
temp=new Date()+30.days//current date 6-1-2016
}
println(sdf.format(temp))
this was the output:
2016-02-36
I tried plus(30) also giving me the same result. Is there a way to do this increment correctly?

In a Java date format, D stands for "Day in Year", hence 6+30 = 36. You want to use d for "Day in month".
You are also using Y which is "Week year" instead of y which is "year" and M for minutes when you want m.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

I'd go with this approach :
Calendar calendar = Calendar.getInstance()
calendar.add(Calendar.DAY_OF_YEAR, 30)
or another approach is to use Joda Time

Related

Adding date & calendar week column in py spark dataframe

I'm using spark 2.4.5. I want to add two new columns, date & calendar week, in my pyspark data frame df.
So I tried the following code:
from pyspark.sql.functions import lit
df.withColumn('timestamp', F.lit('2020-05-01'))
df.show()
But I'm getting error message: AssertionError: col should be Column
Can you explain how to add date column & calendar week?
Looks like you missed the lit function in your code.
Here's what you were looking for:
df = df.withColumn("date", lit('2020-05-01'))
This is your answer if you want to hardcode the date and week. If you want to programmatically derive the current timestamp, I'd recommend using a UDF.
I see two questions here: First, how to cast a string to a date. Second, how to get the week of the year from a date.
Cast string to date
You can either simply use cast("date") or the more specific F.to_date.
df = df.withColumn("date", F.to_date("timestamp", "yyyy-MM-dd"))
Extract week of year
Using format date allows you to format a date column to any desired format. w is the week of the year. W would be the week of the month.
df = df.withColumn("week_of_year", F.date_format("date", "w"))
Related Question: pyspark getting weeknumber of month

Pandas - Remove Timestamp

I am trying to calculate basic statistics using pandas. I have precip values for a whole year from 1956. I created a "Date" column that has date for the entire year using pd.date_range. Then I calculated the max value for the year and the date of maximum value. The date of maximum value show "Timestamp('1956-06-19 00:00:00" as the output. How do I extract just the date. I do not need the timestamp or the 00:00:00 time
#Create Date Column
year = 1956
start_date = datetime.date(year,1,1)
end_date = datetime.date(year,12,31)
precip_file["Date"] = pd.date_range(start=start_date,end=end_date,freq="D")
#Yearly maximum value and date of maximum value
yearly_max = precip_file["Precip (mm)"].max(skipna=True)
max_index = precip_file["Precip (mm)"].idxmax()
yearly_max_date = precip_file.iat[max_index,2
Image of output dictionary I am trying to create
May be a duplicate of this question, although I can't tell whether you are trying to convert one DateTime or a column of DateTimes.

Converting timestamp in a dataframe to date and time in python

In the image I have a dataframe.In that I have a column called timestamp ,from that I want to seperate month and have to make it as a new column.How to do that?
If your Timestamp is not already datetime than convert like so:
df["Timestamp_converted"] = pd.to_datetime(df["Timestamp"], format="%Y-%m-%d %H:%M:%S")
You get the month as a separate column with this:
df["month"] = df.Timestamp_converted.dt.month

save date from date picker into core data

i work with swift 3 for macOS and would like to save my date picker value in core data.
I have an attribute (type date) in my entity.
I tried this code for save my date picker value into core date:
let entity = NSEntityDescription.entity(forEntityName: "Person", in: context)
let newRecord = NSManagedObject(entity: entity!, insertInto: context) as! Person
newRecord.bday = myDatePicker.dateValue as NSDate
This value will be save in CoreData:
517935600
is this correct? oO
Probably. It depends on what date you had selected in the date picker. NSDate and Date store the date as the number of seconds since a reference date, which is the start of January 1, 2000 UTC. It looks like the UTC value from your date picker was May 31 2017 at 9:00 UTC.

Calculate WeekOfMonth & WeekOfYear from a given date in Apache Spark

In one of the case in the my work I need to calculate the week of month and week of year from a give date. In Spark 1.5.0 there is a built in function available to calculate the same.
import org.apache.spark.sql.functions._
val WeekOfMonth = date_format($"GivenDate","W")
val WeekOfYear = weekofyear($"GivenDate")
This value takes start of the week as Sunday.
But I want to calculate the week of month and week of year with Thursday as the start of the week. How can I do that?
I figured out a way to find the Week Of Month and Week Of Year with Thursday as start of the week. Below is the code i used to achieve the same.
import java.sql.Timestamp
import java.text.SimpleDateFormat
import java.util.Calendar
val dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
val dateValue = dateFormat.parse(givenDate)
val cal = Calendar.getInstance()
cal.setTime(dateValue)
cal.setFirstDayOfWeek(Calendar.THURSDAY)
cal.setMinimalDaysInFirstWeek(1)
val weekOfMonth = cal.get(Calendar.WEEK_OF_MONTH)
val weekOfYear = cal.get(Calendar.WEEK_OF_YEAR)
Hope this helps someone. thank you.

Resources