Reputation: 403
Using Spark Scala on Databricks, I am trying to create a variable that contains the first day of the current month.
In the first step, I just get the current date, and it works fine:
val current_date = LocalDate.now()
That gives me the correct output, such as:
current_date: java.time.LocalDate = 2022-05-02
My problem is when I try to get the first day of the current month. I have tried to use TemporalAdjuster, but it doesn't work. Can anyone indicate me the correct way to do this?
Examples that I have tried:
val current_month = current_date.temporal(TemporalAdjuster.firstDayOfMonth())
>>> error: value temporal is not a member of java.time.LocalDate
val current_month = current_date.temporal(TemporalAdjuster.firstDayOfMonth())
>>> error: value firstDayOfMonth is not a member of object java.time.temporal.TemporalAdjuster
val current_month = current_date.temporal(TemporalAdjuster.firstDayOfMonth())
val current_month = current_date.with(TemporalAdjuster.firstDayOfMonth())
>>> error: identifier expected but 'with' found.
val current_month = current_date.with(TemporalAdjuster.firstDayOfMonth())
^
Upvotes: 3
Views: 1983
Reputation: 403
I just had to use the method withDayOfMonth(1)
. The 1
inside the brackets indicates that it must return the first day of month.
The following code works:
val current_month = LocalDate.now().withDayOfMonth(1)
Upvotes: 0
Reputation: 87254
Simplest way is to use date_trunc
function to round a current date to a given granularity. To get first day, you need to truncate current date to a month level, like this (we need to use to_date
because date_trunc
returns a timestamp):
to_date(date_trunc( "mon", current_date()))
For example, if you run it as following on Databricks:
display(spark.range(3).withColumn("first_day",
to_date(date_trunc( "mon", current_date()))))
you'll get
Upvotes: 1