Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.ParseException
- import java.text.SimpleDateFormat
- import java.util.*
- /*
- * Created by 849501 on 12/26/2017.
- */
- object Constants {
- const val SERVER_TIME_STAMP_FORMAT: String = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
- const val UI_TIME_STAMP_FORMAT: String = "dd MMM yyyy h:mm a"
- const val GMT_TIME_ZONE = "GMT"
- }
- fun String.getDateWithServerTimeStamp(): Date? {
- val gmtToLocalFormat = SimpleDateFormat(Constants.SERVER_TIME_STAMP_FORMAT,
- Locale.getDefault())
- gmtToLocalFormat.timeZone = TimeZone.getTimeZone(Constants.GMT_TIME_ZONE)
- try {
- return gmtToLocalFormat.parse(this)
- } catch (e: ParseException) {
- return null
- }
- }
- fun Date.getStringTimeStampWithDate(): String {
- val gmtToLocalFormat = SimpleDateFormat(Constants.SERVER_TIME_STAMP_FORMAT,
- Locale.getDefault())
- gmtToLocalFormat.timeZone = TimeZone.getTimeZone(Constants.GMT_TIME_ZONE)
- return gmtToLocalFormat.format(this)
- }
- fun Date?.getUITimeStampWithDate(): String {
- if (this == null) return "NA"
- val gmtToLocalFormat = SimpleDateFormat(Constants.UI_TIME_STAMP_FORMAT,
- Locale.getDefault())
- gmtToLocalFormat.timeZone = TimeZone.getDefault()
- return gmtToLocalFormat.format(this)
- }
Add Comment
Please, Sign In to add comment