Spark SQL provide a very convenient function named next_day
since version 1.5.0. It can be used to calculate the next date that is after a certain date with specified name.
Syntax
next_day(start_date, day_of_week)
The function returns the first date which is later than start_date
and named as indicated by day_of_week
.
Code snippet
The following examples calculate any day of the week after.
spark-sql> select next_day(DATE'2022-06-16', 'Monday');
2022-06-20
spark-sql> select next_day(DATE'2022-06-16', 'TU');
2022-06-21
spark-sql> select next_day(DATE'2022-06-16', 'WE');
2022-06-22
spark-sql> select next_day(DATE'2022-06-16', 'TH');
2022-06-23
spark-sql> select next_day(DATE'2022-06-16', 'FR');
2022-06-17
spark-sql> select next_day(DATE'2022-06-16', 'FRIDAY');
2022-06-17
spark-sql> select next_day(DATE'2022-06-16', 'Sat');
2022-06-18
spark-sql> select next_day(DATE'2022-06-16', 'Sun');
2022-06-19
The function returns the right date per calendar: