0

I have three columns - "Year","Month" and "Hour". I need inputs on converting this to a timestamp in spark scala.

Example: my year is 2017, month is 11 and hour is 570, I need to convert this to a timestamp in Spark Scala.

Any inputs on this will be of great help. The output should be 2017-11-23 17:00

  • If you do not have the time as input, you cannot create a timestamp... – Fernando Aspiazu Oct 03 '18 at 20:47
  • i have another question with this, given a month I need to know the number of days in a month! So that I convert those days to hours instead of having to convert to a timestamp. What are your thoughts on this? @FernandoAspiazu – Deepika Jantz Oct 03 '18 at 20:49
  • @DeepikaJantz may be you can look into a 3rd party API for calender which returns u this data based on the month and year passed to it ? – zenwraight Oct 03 '18 at 20:50
  • Now number of days in a month stays constant except for feb, and that you can calculate whether the year is leap year or not. Jan has 31 , March has 31 , april has 30 only feb has between 28 or 29, and based on year u can set days for feb. hope this helps. – zenwraight Oct 03 '18 at 20:51
  • For number of days in a month use java.time (too): [`YearMonth.lengthOfMonth`](https://docs.oracle.com/javase/10/docs/api/java/time/YearMonth.html#lengthOfMonth()). – Ole V.V. Oct 04 '18 at 13:11

2 Answers2

3

Java's LocalDateTime should do it for you

import java.time.LocalDateTime
val y = 2017
val m = 11
val h = 570
val d1 = LocalDateTime.of(y, m, 1, 0, 0) // have to use 1 for day
val d2 = d1.plusHours(h)

I do get a different result than you though. The above gets me:

d2: java.time.LocalDateTime = 2017-11-24T18:00

Andrew
  • 4,574
  • 26
  • 31
-1
    def getTimeStamp(y:Int,m:Int,h:Int) = {
     require(m<=12,"Invalid Data")
     def leapYr(y:Int) = if(y%100==0) y%400==0 else y%4==0
     val monthDays = Array((1,31),(2,28),(3,31),(4,30),(5,31),(6,30),
                            (7,31),(8,31),(9,30),(10,31),(11,30),(12,31))
     val monthDaysLeap = Array((1,31),(2,29),(3,31),(4,30),(5,31),(6,30),
                            (7,31),(8,31),(9,30),(10,31),(11,30),(12,31))
     val days = if(!leapYr(y)){ monthDays(m-1)._2} else { monthDaysLeap(m-1)._2 }
     val hoursInMonth = days*24
     if(h<=hoursInMonth){ val d:Double = h/24.0; val d1=d.toInt;
          val hr = (24*(d-d1)).toInt; y+"-"+m+"-"+d1+" "+hr+":"+"00" 
     }
     else "Invalid Data"
    }

Test:

scala> getTimeStamp(2017,11,570)
res36: String = 2017-11-23 18:00
RAGHHURAAMM
  • 1,099
  • 7
  • 15