'How to create variable with the first day of current month in Databricks Spark Scala?
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())
^
Solution 1:[1]
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)
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rafael Pinheiro |
| Solution 2 | Alex Ott |

