Created
October 21, 2020 09:48
-
-
Save nadar71/9536d9ccfb5c8b895ec1b0db46625f25 to your computer and use it in GitHub Desktop.
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
import java.text.SimpleDateFormat | |
import java.util.* | |
// Converting from Date to String | |
fun Date.getStringTimeStampWithDate(): String { | |
val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", | |
Locale.ENGLISH) | |
// dateFormat.timeZone = TimeZone.getTimeZone("UTC") | |
return dateFormat.format(this) | |
} | |
// Converting to server timestamp current format | |
fun Date.getDateTimeStampFormat(): String { | |
val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", | |
Locale.ENGLISH) | |
// dateFormat.timeZone = TimeZone.getTimeZone("UTC") | |
return dateFormat.format(this) | |
} | |
// Getting HH:mm string from Date | |
fun Date.getHHmmFromDate(): String{ | |
val cal = Calendar.getInstance() | |
cal.time = this | |
val hours = cal.get(Calendar.HOUR_OF_DAY) | |
var minutes = cal.get(Calendar.MINUTE).toString() | |
if (minutes.length <= 1) minutes = "0${minutes}" | |
return "${hours}:${minutes}" | |
} | |
// Getting DD-MM-YYYY string from Date | |
fun Date.getDDMMYYYYFromDate(): String{ | |
val cal = Calendar.getInstance() | |
cal.time = this | |
val day = cal.get(Calendar.DAY_OF_MONTH) | |
var month = (cal.get(Calendar.MONTH)+1).toString() | |
if (month.length <= 1) month = "0$month" | |
val year = cal.get(Calendar.YEAR) | |
return "${day}-${month}-${year}" | |
} | |
// Getting DD MMM string from Date | |
fun Date.getDDMMMFromDate(): String{ | |
val cal = Calendar.getInstance() | |
cal.time = this | |
val day = cal.get(Calendar.DAY_OF_MONTH) | |
val month = SimpleDateFormat("MMM", Locale.getDefault()).format(cal.time) | |
return "${day} ${month}" | |
} | |
// Getting DD-MM-YYYY string from Date | |
fun Date.getRealDDMMYYYYFromDate(): String{ | |
val cal = Calendar.getInstance() | |
cal.time = this | |
var day = cal.get(Calendar.DAY_OF_MONTH).toString() | |
var month = (cal.get(Calendar.MONTH)+1).toString() | |
if(day.length <= 1 ) day = "0$day" | |
if (month.length <= 1) month = "0$month" | |
val year = cal.get(Calendar.YEAR) | |
return "${day}-${month}-${year}" | |
} | |
// Getting hours and minute from date in minutes | |
fun Date.getMinuteOfDayFromDate(): Int{ | |
val cal = Calendar.getInstance() | |
cal.time = this | |
val hours = cal.get(Calendar.HOUR_OF_DAY) | |
val minutes = cal.get(Calendar.MINUTE) | |
return (hours*60 + minutes) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment