Created
February 15, 2021 13:56
-
-
Save abircse/01deb3a94ea8be2e9d0059447052992c to your computer and use it in GitHub Desktop.
DateTimeUtilsExtension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object DateTimeUtils { | |
fun convertDateToLong(date : String) : Long | |
{ | |
val dateFormat = SimpleDateFormat("yyyy/MM/dd") | |
val date = dateFormat.parse(date) | |
val dateInLong = date.time | |
return dateInLong | |
} | |
fun convertCurrentDateToLong() : Long | |
{ | |
val dateFormat = SimpleDateFormat("yyyy/MM/dd") | |
val data = dateFormat.format(Date()) | |
val date = dateFormat.parse(data) | |
val dateInLong = date.time | |
return dateInLong | |
} | |
fun getCurrentDateTime(): String? { | |
val sdf = SimpleDateFormat("yyyy.MM.dd HH:mm:ss") | |
return sdf.format(Date()) | |
} | |
fun getCurrentTimeDateInLong(): String? { | |
val sdf = SimpleDateFormat("yyyy.MM.dd HH:mm:ss") | |
var datatimeinmills: String? = null | |
try { | |
val currentDateandTime = sdf.format(Date()) | |
val d = sdf.parse(currentDateandTime) | |
val milliseconds = d.time | |
datatimeinmills = java.lang.Long.toString(milliseconds) | |
} catch (e: ParseException) { | |
e.printStackTrace() | |
} | |
return datatimeinmills | |
} | |
// long to string convert | |
fun getCurrentDateInLongToString(): String? { | |
val sdf = SimpleDateFormat("yyyy-MM-dd") | |
var datatimeinmills: String? = null | |
try { | |
val currentDateandTime = sdf.format(Date()) | |
val d = sdf.parse(currentDateandTime) | |
val milliseconds = d.time | |
datatimeinmills = java.lang.Long.toString(milliseconds) | |
} catch (e: ParseException) { | |
e.printStackTrace() | |
} | |
return datatimeinmills | |
} | |
fun convertCurrentDateInLongToString(mydate: String?): String? { | |
val f = SimpleDateFormat("yyyy-MM-dd") | |
var parseDate: Date? = null | |
val dateinmills: String | |
try { | |
parseDate = f.parse(mydate) | |
} catch (e: ParseException) { | |
e.printStackTrace() | |
} | |
val milliseconds = parseDate!!.time | |
dateinmills = java.lang.Long.toString(milliseconds) | |
return dateinmills | |
} | |
// Long direct Value getter method | |
fun longcurrentdate(): Long { | |
val sdf = SimpleDateFormat("yyyy-MM-dd") | |
var milliseconds: Long = 0 | |
try { | |
val currentDateandTime = sdf.format(Date()) | |
val d = sdf.parse(currentDateandTime) | |
milliseconds = d.time | |
} catch (e: ParseException) { | |
e.printStackTrace() | |
} | |
return milliseconds | |
} | |
fun convertlonganydate(mydate: String?): Long { | |
val f = SimpleDateFormat("yyyy-MM-dd") | |
var parseDate: Date? = null | |
try { | |
parseDate = f.parse(mydate) | |
} catch (e: ParseException) { | |
e.printStackTrace() | |
} | |
return parseDate!!.time | |
} | |
fun getCurrentDateTimeInRealFormat(): String { | |
val sdf = SimpleDateFormat("h:mm a dd MMMM yyyy") | |
return sdf.format(Date()) | |
} | |
fun gettimeasUnix(): Double { | |
return System.currentTimeMillis() / 1000L.toDouble() | |
} | |
fun convertunixtimetooriginalformat(unixtime: Double): String? { | |
val date = Date((unixtime * 1000L).toLong()) | |
// format of the date | |
val jdf = SimpleDateFormat("h:mm a dd MMMM yyyy") | |
return jdf.format(date) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment