Skip to content

Instantly share code, notes, and snippets.

@tush4r
Created December 5, 2016 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tush4r/ee08f7f98d24c61a13203c636df144b7 to your computer and use it in GitHub Desktop.
Save tush4r/ee08f7f98d24c61a13203c636df144b7 to your computer and use it in GitHub Desktop.
import org.joda.time.format.DateTimeFormat
import org.joda.time.{DateTime, DateTimeZone}
import scala.util.Try
object TimeStampUtility {
def getCurrentTimeStamp(): Long = {
val timestamp: Long = System.currentTimeMillis / 1000
timestamp
}
def isGreaterThanCurrentTimeStamp(timestamp:Long): Boolean = {
val currentTimestamp: Long = getCurrentTimeStamp().toLong
val isGivenTimeStampValid = Try(timestamp.toLong).isSuccess
var booleanTimeStamp = false
if (isGivenTimeStampValid) {
if (currentTimestamp < timestamp.toLong) {
booleanTimeStamp = true
}
}
booleanTimeStamp
}
def addToCurrentTimeStamp(timeInSeconds:Long): Long = {
val currentTimeStamp = getCurrentTimeStamp()
val newTimeStamp = timeInSeconds + currentTimeStamp
newTimeStamp
}
def convertEpochToDateTime(epochTimeInSeconds:Long): String = {
val currentTimeZone = DateTimeZone.getDefault()
val currentEpoch = getCurrentTimeStamp()
val currentTime = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss").print(currentEpoch*1000)
currentTime
}
def convertDateTimeToEpoch(dateTime_YYYY_MM_dd_HH_mm_SS:String): Long = {
val date = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss").parseDateTime(dateTime_YYYY_MM_dd_HH_mm_SS)
date.getMillis
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment