Guest User

Untitled

a guest
Jan 19th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. import java.text.ParseException
  2. import java.text.SimpleDateFormat
  3. import java.util.*
  4.  
  5. /*
  6. * Created by 849501 on 12/26/2017.
  7. */
  8.  
  9. object Constants {
  10. const val SERVER_TIME_STAMP_FORMAT: String = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
  11. const val UI_TIME_STAMP_FORMAT: String = "dd MMM yyyy h:mm a"
  12. const val GMT_TIME_ZONE = "GMT"
  13. }
  14.  
  15. fun String.getDateWithServerTimeStamp(): Date? {
  16. val gmtToLocalFormat = SimpleDateFormat(Constants.SERVER_TIME_STAMP_FORMAT,
  17. Locale.getDefault())
  18. gmtToLocalFormat.timeZone = TimeZone.getTimeZone(Constants.GMT_TIME_ZONE)
  19. try {
  20. return gmtToLocalFormat.parse(this)
  21. } catch (e: ParseException) {
  22. return null
  23. }
  24. }
  25.  
  26. fun Date.getStringTimeStampWithDate(): String {
  27. val gmtToLocalFormat = SimpleDateFormat(Constants.SERVER_TIME_STAMP_FORMAT,
  28. Locale.getDefault())
  29. gmtToLocalFormat.timeZone = TimeZone.getTimeZone(Constants.GMT_TIME_ZONE)
  30. return gmtToLocalFormat.format(this)
  31. }
  32.  
  33. fun Date?.getUITimeStampWithDate(): String {
  34. if (this == null) return "NA"
  35.  
  36. val gmtToLocalFormat = SimpleDateFormat(Constants.UI_TIME_STAMP_FORMAT,
  37. Locale.getDefault())
  38. gmtToLocalFormat.timeZone = TimeZone.getDefault()
  39. return gmtToLocalFormat.format(this)
  40. }
Add Comment
Please, Sign In to add comment