Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.06 KB | None | 0 0
  1. object CalendarUtils {
  2. val yyyyFormat: String = "yyyy"
  3. val MMMMdFormat: String = "MMMM d"
  4. val MMMdFormat: String = "MMM d"
  5. val DMMMFormat: String = "d MMM"
  6. val MMMdyyyyFormat: String = "MMM d, yyyy"
  7. val MMMMdyyyyFormat: String = "MMMM d, yyyy"
  8. val mddyyyyFormat: String = "M/dd/yyyy"
  9. val yyyyMMddFormat: String = "yyyy-MM-dd"
  10. val HHmmssFormat: String = "hh:mm:ss a"
  11. val yyyyMMddHHmmssFormat: String = "yyyy-MM-dd HH:mm:ss"
  12. val yyyyMMddHHmmssSSSFormat: String = "yyyy-MM-dd HH:mm:ss SSS"
  13. val ddMMMFormat: String = "dd'\n'MMM"
  14. val hhmmFormat: String = "hh:mm'\n'a"
  15. val MMMMddyyyy_hhmmaFormat: String = "MMMM dd, yyyy hh:mm a"
  16. val MMMddyyyy_HHmmFormat: String = "MMM dd, yyyy HH:mm"
  17. val MMMdyyyy_HmFormat: String = "MMM d, yyyy H:m"
  18. val MMMddyyyyFormat: String = "MMM dd, yyyy"
  19. val MMdd: String = "MM-dd" // for birthdate comparision
  20. val dMMMyyyy: String = "d MMM yyyy" // for birthdate comparision
  21. val ddMMyyyy_HH_mm_ss: String = "dd/MM/yyyy HH:mm:ss" // for birthdate comparision
  22.  
  23. fun dateDifference(selectedDate: Calendar, currentDate: Calendar = Calendar.getInstance()): Long? {
  24.  
  25. currentDate.set(Calendar.HOUR_OF_DAY, 0)
  26. currentDate.set(Calendar.MINUTE, 0)
  27. currentDate.set(Calendar.SECOND, 0)
  28. currentDate.set(Calendar.MILLISECOND, 0)
  29.  
  30. selectedDate.set(Calendar.HOUR_OF_DAY, 0)
  31. selectedDate.set(Calendar.MINUTE, 0)
  32. selectedDate.set(Calendar.SECOND, 0)
  33. selectedDate.set(Calendar.MILLISECOND, 0)
  34.  
  35. // At this point, each calendar is set to midnight on
  36. // their respective days. Now use TimeUnit.MILLISECONDS to
  37. // compute the number of full days between the two of them.
  38. return TimeUnit.MILLISECONDS.toDays(Math.abs(selectedDate.timeInMillis) - Math.abs(currentDate.timeInMillis))
  39. }
  40.  
  41. fun format(ipFormat: String, date: String, opFormat: String): String {
  42. val dateFormat = SimpleDateFormat(ipFormat, Locale.US)
  43. val parsedDate = dateFormat.parse(date)
  44. val opDateFormat = SimpleDateFormat(opFormat, Locale.US);
  45. return opDateFormat.format(parsedDate);
  46. }
  47.  
  48. fun format(date: Date, format: String, timezone: TimeZone): String {
  49. val dateFormat = SimpleDateFormat(format, Locale.US)
  50. dateFormat.timeZone = timezone
  51. return dateFormat.format(date)
  52. }
  53.  
  54. fun format(date: Date, format: String): String {
  55. val dateFormat = SimpleDateFormat(format, Locale.US)
  56. return dateFormat.format(date);
  57. }
  58.  
  59. fun format(format: String): String = format(Date(), format);
  60.  
  61. fun parse(date: String, format: String): Date {
  62. val dateFormat = SimpleDateFormat(format, Locale.US)
  63. return dateFormat.parse(date);
  64. }
  65.  
  66. fun parse(date: String, format: String, timezone: String): Date {
  67. val dateFormat = SimpleDateFormat(format, Locale.US)
  68. dateFormat.timeZone = TimeZone.getTimeZone(timezone)
  69. return dateFormat.parse(date);
  70. }
  71.  
  72. fun getUTCDate() = getDate("UTC")
  73.  
  74. fun getDate(timezone: String): Date {
  75. val calendar = Calendar.getInstance()
  76. calendar.timeZone = TimeZone.getTimeZone(timezone)
  77. return calendar.time
  78. }
  79.  
  80. fun isFutureDate(dateTime: String?): Boolean {
  81. return !isPastDate(dateTime)
  82. }
  83.  
  84. fun isPastDate(dateTime: String?): Boolean {
  85. if (dateTime == null) return false
  86.  
  87. val meetingDateTime = parse(dateTime, yyyyMMddHHmmssFormat)
  88. val currentDateTime = Date()
  89.  
  90. return meetingDateTime.before(currentDateTime)
  91. }
  92.  
  93. fun isToday(timestamp: Long): Boolean {
  94. val calendar = Calendar.getInstance()
  95. calendar.timeInMillis = timestamp
  96.  
  97. val now = Calendar.getInstance()
  98.  
  99. return calendar.get(Calendar.YEAR) == now.get(Calendar.YEAR)
  100. && calendar.get(Calendar.MONTH) == now.get(Calendar.MONTH)
  101. && calendar.get(Calendar.DAY_OF_MONTH) == now.get(Calendar.DAY_OF_MONTH)
  102. }
  103.  
  104. fun formatHours(taskHours: String): String {
  105. return DecimalFormat("0.00").format(taskHours.toFloat())
  106. }
  107.  
  108. fun formatMeetingHours(hours: Float): String {
  109. val time = hours.toDouble() //2.5
  110. val hr = Math.floor(time).toLong() //2
  111. val diff = time - hr
  112. val min = (diff * 60).toLong() //0.5 * 60 => 30
  113. return if (hr == 1L && min == 0L) {
  114. "$hr.0 Hour"
  115. } else if (hr >= 1) {
  116. "$hours Hours"
  117. } else {
  118. if (min > 1) {
  119. "$min Minutes"
  120. } else {
  121. "$min Minute"
  122. }
  123. }
  124. }
  125.  
  126. fun getPast2MonthsDate(): String {
  127. val instance = Calendar.getInstance()
  128. instance.add(Calendar.MONTH, -2)
  129. return CalendarUtils.format(instance.time, CalendarUtils.yyyyMMddFormat)
  130. }
  131.  
  132. fun getNextYearDate(): String {
  133. val instance = Calendar.getInstance()
  134. instance.add(Calendar.YEAR, 1)
  135. return CalendarUtils.format(instance.time, CalendarUtils.yyyyMMddFormat)
  136. }
  137.  
  138. /**
  139. * Get Meeting Duration in Millis
  140. */
  141. fun parseMeetingHours(hours: String): Long {
  142. val time = hours.toDouble() //2.5
  143. val hr = Math.floor(time).toLong() //2
  144. val diff = time - hr
  145. val min = (diff * 60).toLong() //0.5 * 60 => 30
  146. return TimeUnit.HOURS.toMillis(hr) + TimeUnit.MINUTES.toMillis(min)
  147. }
  148.  
  149. fun getCurrentTimeInUTC(): Date? {
  150. val calendar = Calendar.getInstance().apply {
  151. timeZone = TimeZone.getTimeZone("UTC")
  152. timeInMillis -= TimeZone.getDefault().rawOffset
  153. }
  154. return calendar.time
  155. }
  156.  
  157. fun getYearStartCalendar(): Calendar {
  158. return getDayCalendar().apply {
  159. set(Calendar.MONTH, 0)
  160. set(Calendar.DATE, 1)
  161. }
  162. }
  163.  
  164. fun getYearEndCalendar(): Calendar {
  165. return getDayCalendar().apply {
  166. set(Calendar.MONTH, 11)
  167. set(Calendar.DATE, 31)
  168. set(Calendar.HOUR_OF_DAY, 23)
  169. set(Calendar.MINUTE, 59)
  170. set(Calendar.SECOND, 59)
  171. }
  172. }
  173.  
  174. fun getDayCalendar(timeMillis: Long = 0): Calendar {
  175. return Calendar.getInstance().apply {
  176. if (timeMillis != 0L) {
  177. timeInMillis = timeMillis
  178. }
  179.  
  180. set(Calendar.HOUR_OF_DAY, 0)
  181. set(Calendar.MINUTE, 0)
  182. set(Calendar.SECOND, 0)
  183. set(Calendar.MILLISECOND, 0)
  184. }
  185. }
  186.  
  187. fun toCalendar(date: LocalDate): Calendar {
  188. return Calendar.getInstance().apply {
  189. set(Calendar.DAY_OF_MONTH, date.dayOfMonth)
  190. set(Calendar.MONTH, date.monthValue - 1)
  191. set(Calendar.YEAR, date.year)
  192. }
  193. }
  194.  
  195. fun toCalendar(strDate: String, dateFormat: String): Calendar {
  196. val simpleDateFormat = SimpleDateFormat(dateFormat)
  197. val date = simpleDateFormat.parse(strDate)
  198. return Calendar.getInstance().apply {
  199. time = date
  200. }
  201. }
  202.  
  203. fun toLocalDate(millis: Long): LocalDate {
  204. val instant = Instant.ofEpochMilli(millis)
  205. return instant.atZone(ZoneId.systemDefault()).toLocalDate()
  206. }
  207.  
  208. fun getYearDifference(millis: Long): String {
  209. val day = toLocalDate(millis)
  210. val today = LocalDate.now()
  211. return when (val year = today.year - day.year) {
  212. 0 -> "Today"
  213. 1 -> "1 Year Ago"
  214. else -> "$year Years Ago"
  215. }
  216. }
  217.  
  218. /**
  219. * Convert Date Formate
  220. */
  221. fun getServerFormat(date: Calendar): String {
  222. val simpleFormat: SimpleDateFormat = SimpleDateFormat(yyyyMMddFormat)
  223. return simpleFormat.format(date.time)
  224. }
  225.  
  226. fun getFormattedLeaveDuration(startDate: String, endDate: String): String {
  227. val startYear = format(yyyyMMddFormat, startDate, yyyyFormat)
  228. val endYear = format(yyyyMMddFormat, endDate, yyyyFormat)
  229. if (startYear == endYear) {
  230. val startTime = format(yyyyMMddFormat, startDate, MMMdFormat)
  231. val endTime = format(yyyyMMddFormat, endDate, MMMdyyyyFormat)
  232. return "$startTime to $endTime"
  233. } else {
  234. return "$startDate - $endDate"
  235. }
  236. }
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement