Guest User

Untitled

a guest
Nov 22nd, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. import java.text.SimpleDateFormat
  2. import java.util.*
  3.  
  4.  
  5. /**
  6. * Pattern: yyyy-MM-dd HH:mm:ss
  7. */
  8. fun Date.formatToServerDateTimeDefaults(): String{
  9. val sdf= SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
  10. return sdf.format(this)
  11. }
  12.  
  13. fun Date.formatToTruncatedDateTime(): String{
  14. val sdf= SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault())
  15. return sdf.format(this)
  16. }
  17.  
  18. /**
  19. * Pattern: yyyy-MM-dd
  20. */
  21. fun Date.formatToServerDateDefaults(): String{
  22. val sdf= SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
  23. return sdf.format(this)
  24. }
  25.  
  26. /**
  27. * Pattern: HH:mm:ss
  28. */
  29. fun Date.formatToServerTimeDefaults(): String{
  30. val sdf= SimpleDateFormat("HH:mm:ss", Locale.getDefault())
  31. return sdf.format(this)
  32. }
  33.  
  34. /**
  35. * Pattern: dd/MM/yyyy HH:mm:ss
  36. */
  37. fun Date.formatToViewDateTimeDefaults(): String{
  38. val sdf= SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.getDefault())
  39. return sdf.format(this)
  40. }
  41.  
  42. /**
  43. * Pattern: dd/MM/yyyy
  44. */
  45. fun Date.formatToViewDateDefaults(): String{
  46. val sdf= SimpleDateFormat("dd/MM/yyyy", Locale.getDefault())
  47. return sdf.format(this)
  48. }
  49.  
  50. /**
  51. * Pattern: HH:mm:ss
  52. */
  53. fun Date.formatToViewTimeDefaults(): String{
  54. val sdf= SimpleDateFormat("HH:mm:ss", Locale.getDefault())
  55. return sdf.format(this)
  56. }
  57.  
  58. /**
  59. * Add field date to current date
  60. */
  61. fun Date.add(field: Int, amount: Int): Date{
  62. val cal = Calendar.getInstance()
  63. cal.time=this
  64. cal.add(field, amount)
  65.  
  66. this.time = cal.time.time
  67.  
  68. cal.clear()
  69.  
  70. return this
  71. }
  72.  
  73. fun Date.addYears(years: Int): Date{
  74. return add(Calendar.YEAR, years)
  75. }
  76. fun Date.addMonths(months: Int): Date {
  77. return add(Calendar.MONTH, months)
  78. }
  79. fun Date.addDays(days: Int): Date{
  80. return add(Calendar.DAY_OF_MONTH, days)
  81. }
  82. fun Date.addHours(hours: Int): Date{
  83. return add(Calendar.HOUR_OF_DAY, hours)
  84. }
  85. fun Date.addMinutes(minutes: Int): Date{
  86. return add(Calendar.MINUTE, minutes)
  87. }
  88. fun Date.addSeconds(seconds: Int): Date{
  89. return add(Calendar.SECOND, seconds)
  90. }
Add Comment
Please, Sign In to add comment