Sekar Ramu
Sekar Ramu

Reputation: 483

Scala : day of week from Epochtime

I am trying to get the day of week from the epoch time.

I am able to get the day using time stamp but not able to get the same using the epochtime instead of timestamp.

Below are the things which I tried:

import java.time.LocalDate
import java.time.format.DateTimeFormatter
val df = DateTimeFormatter.ofPattern("dd/MM/yyyy")
val dayOfWeek = LocalDate.parse("21/05/2017",df).getDayOfWeek

dayOfWeek: java.time.DayOfWeek = SUNDAY

I am looking for something like below where I am able to get week day as INT. for example Monday as 1 and so on but using the epoch time instead of timestamp. Sample epochtime : 1576683390

 import spark.sqlContext.implicits._

  val df = Seq(("2019-07-01 12:01:19.000"),
("2019-06-24 12:01:19.000"),
("2019-11-16 16:44:55.406"),
("2019-11-16 16:50:59.406")).toDF("input_timestamp")

df.withColumn("input_timestamp",
to_timestamp(col("input_timestamp"))).withColumn("week_day_number", date_format(col("input_timestamp"), "u")).withColumn("week_day_abb", date_format(col("input_timestamp"), "E")).show(false)

+-----------------------+---------------+------------+
|input_timestamp        |week_day_number|week_day_abb|
+-----------------------+---------------+------------+
|2019-07-01 12:01:19    |1              |Mon         |
|2019-06-24 12:01:19    |1              |Mon         |
|2019-11-16 16:44:55.406|6              |Sat         |
|2019-11-16 16:50:59.406|6              |Sat         |
+-----------------------+---------------+------------+

Thanks in advance !!!

Upvotes: 0

Views: 788

Answers (1)

baitmbarek
baitmbarek

Reputation: 2518

Sekar, you can cast your epoch to a Timestamp if I correctly understood your question :

import spark.implicits._
import org.apache.spark.sql.functions._

val df = Seq(1576683390).toDF("input_timestamp")

df.withColumn("input_timestamp", $"input_timestamp".cast(TimestampType))
  .withColumn("week_day_number", date_format(col("input_timestamp"), "u"))
  .withColumn("week_day_abb", date_format(col("input_timestamp"), "E"))
  .show(false)

+---------------------+---------------+------------+
|input_timestamp      |week_day_number|week_day_abb|
+---------------------+---------------+------------+
|2019-12-18 16:36:30.0|3              |Wed         |
+---------------------+---------------+------------+

EDIT :

With simple Sequence and LocalDateTime :

import java.time.{Instant, LocalDateTime, ZoneOffset}

val seq = Seq(1576683390)

seq.map{epoch =>
  val dayOfWeek = LocalDateTime.ofInstant(Instant.ofEpochSecond(epoch), ZoneOffset.UTC).getDayOfWeek
  (dayOfWeek.getValue, dayOfWeek.toString)
}.foreach{println}

Output : (3,WEDNESDAY)

Upvotes: 3

Related Questions