Last active
June 13, 2019 07:08
-
-
Save hitesh-dhamshaniya/5cb0b5281f56651996738efdf0a75d32 to your computer and use it in GitHub Desktop.
Class is used for date related operation, i.e change date format, convert date string object to date object, check past and future date and many more functions are there.
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
| object CalendarUtils { | |
| val yyyyFormat: String = "yyyy" | |
| val MMMMdFormat: String = "MMMM d" | |
| val MMMdFormat: String = "MMM d" | |
| val DMMMFormat: String = "d MMM" | |
| val MMMdyyyyFormat: String = "MMM d, yyyy" | |
| val MMMMdyyyyFormat: String = "MMMM d, yyyy" | |
| val mddyyyyFormat: String = "M/dd/yyyy" | |
| val yyyyMMddFormat: String = "yyyy-MM-dd" | |
| val HHmmssFormat: String = "hh:mm:ss a" | |
| val yyyyMMddHHmmssFormat: String = "yyyy-MM-dd HH:mm:ss" | |
| val yyyyMMddHHmmssSSSFormat: String = "yyyy-MM-dd HH:mm:ss SSS" | |
| val ddMMMFormat: String = "dd'\n'MMM" | |
| val hhmmFormat: String = "hh:mm'\n'a" | |
| val MMMMddyyyy_hhmmaFormat: String = "MMMM dd, yyyy hh:mm a" | |
| val MMMddyyyy_HHmmFormat: String = "MMM dd, yyyy HH:mm" | |
| val MMMdyyyy_HmFormat: String = "MMM d, yyyy H:m" | |
| val MMMddyyyyFormat: String = "MMM dd, yyyy" | |
| val MMdd: String = "MM-dd" // for birthdate comparision | |
| val dMMMyyyy: String = "d MMM yyyy" // for birthdate comparision | |
| val ddMMyyyy_HH_mm_ss: String = "dd/MM/yyyy HH:mm:ss" // for birthdate comparision | |
| fun dateDifference(selectedDate: Calendar, currentDate: Calendar = Calendar.getInstance()): Long? { | |
| currentDate.set(Calendar.HOUR_OF_DAY, 0) | |
| currentDate.set(Calendar.MINUTE, 0) | |
| currentDate.set(Calendar.SECOND, 0) | |
| currentDate.set(Calendar.MILLISECOND, 0) | |
| selectedDate.set(Calendar.HOUR_OF_DAY, 0) | |
| selectedDate.set(Calendar.MINUTE, 0) | |
| selectedDate.set(Calendar.SECOND, 0) | |
| selectedDate.set(Calendar.MILLISECOND, 0) | |
| // At this point, each calendar is set to midnight on | |
| // their respective days. Now use TimeUnit.MILLISECONDS to | |
| // compute the number of full days between the two of them. | |
| return TimeUnit.MILLISECONDS.toDays(Math.abs(selectedDate.timeInMillis) - Math.abs(currentDate.timeInMillis)) | |
| } | |
| fun format(ipFormat: String, date: String, opFormat: String): String { | |
| val dateFormat = SimpleDateFormat(ipFormat, Locale.US) | |
| val parsedDate = dateFormat.parse(date) | |
| val opDateFormat = SimpleDateFormat(opFormat, Locale.US); | |
| return opDateFormat.format(parsedDate); | |
| } | |
| fun format(date: Date, format: String, timezone: TimeZone): String { | |
| val dateFormat = SimpleDateFormat(format, Locale.US) | |
| dateFormat.timeZone = timezone | |
| return dateFormat.format(date) | |
| } | |
| fun format(date: Date, format: String): String { | |
| val dateFormat = SimpleDateFormat(format, Locale.US) | |
| return dateFormat.format(date); | |
| } | |
| fun format(format: String): String = format(Date(), format); | |
| fun parse(date: String, format: String): Date { | |
| val dateFormat = SimpleDateFormat(format, Locale.US) | |
| return dateFormat.parse(date); | |
| } | |
| fun parse(date: String, format: String, timezone: String): Date { | |
| val dateFormat = SimpleDateFormat(format, Locale.US) | |
| dateFormat.timeZone = TimeZone.getTimeZone(timezone) | |
| return dateFormat.parse(date); | |
| } | |
| fun getUTCDate() = getDate("UTC") | |
| fun getDate(timezone: String): Date { | |
| val calendar = Calendar.getInstance() | |
| calendar.timeZone = TimeZone.getTimeZone(timezone) | |
| return calendar.time | |
| } | |
| fun isFutureDate(dateTime: String?): Boolean { | |
| return !isPastDate(dateTime) | |
| } | |
| fun isPastDate(dateTime: String?): Boolean { | |
| if (dateTime == null) return false | |
| val meetingDateTime = parse(dateTime, yyyyMMddHHmmssFormat) | |
| val currentDateTime = Date() | |
| return meetingDateTime.before(currentDateTime) | |
| } | |
| fun isToday(timestamp: Long): Boolean { | |
| val calendar = Calendar.getInstance() | |
| calendar.timeInMillis = timestamp | |
| val now = Calendar.getInstance() | |
| return calendar.get(Calendar.YEAR) == now.get(Calendar.YEAR) | |
| && calendar.get(Calendar.MONTH) == now.get(Calendar.MONTH) | |
| && calendar.get(Calendar.DAY_OF_MONTH) == now.get(Calendar.DAY_OF_MONTH) | |
| } | |
| fun formatHours(taskHours: String): String { | |
| return DecimalFormat("0.00").format(taskHours.toFloat()) | |
| } | |
| fun formatMeetingHours(hours: Float): String { | |
| val time = hours.toDouble() //2.5 | |
| val hr = Math.floor(time).toLong() //2 | |
| val diff = time - hr | |
| val min = (diff * 60).toLong() //0.5 * 60 => 30 | |
| return if (hr == 1L && min == 0L) { | |
| "$hr.0 Hour" | |
| } else if (hr >= 1) { | |
| "$hours Hours" | |
| } else { | |
| if (min > 1) { | |
| "$min Minutes" | |
| } else { | |
| "$min Minute" | |
| } | |
| } | |
| } | |
| fun getPast2MonthsDate(): String { | |
| val instance = Calendar.getInstance() | |
| instance.add(Calendar.MONTH, -2) | |
| return CalendarUtils.format(instance.time, CalendarUtils.yyyyMMddFormat) | |
| } | |
| fun getNextYearDate(): String { | |
| val instance = Calendar.getInstance() | |
| instance.add(Calendar.YEAR, 1) | |
| return CalendarUtils.format(instance.time, CalendarUtils.yyyyMMddFormat) | |
| } | |
| /** | |
| * Get Meeting Duration in Millis | |
| */ | |
| fun parseMeetingHours(hours: String): Long { | |
| val time = hours.toDouble() //2.5 | |
| val hr = Math.floor(time).toLong() //2 | |
| val diff = time - hr | |
| val min = (diff * 60).toLong() //0.5 * 60 => 30 | |
| return TimeUnit.HOURS.toMillis(hr) + TimeUnit.MINUTES.toMillis(min) | |
| } | |
| fun getCurrentTimeInUTC(): Date? { | |
| val calendar = Calendar.getInstance().apply { | |
| timeZone = TimeZone.getTimeZone("UTC") | |
| timeInMillis -= TimeZone.getDefault().rawOffset | |
| } | |
| return calendar.time | |
| } | |
| fun getYearStartCalendar(): Calendar { | |
| return getDayCalendar().apply { | |
| set(Calendar.MONTH, 0) | |
| set(Calendar.DATE, 1) | |
| } | |
| } | |
| fun getYearEndCalendar(): Calendar { | |
| return getDayCalendar().apply { | |
| set(Calendar.MONTH, 11) | |
| set(Calendar.DATE, 31) | |
| set(Calendar.HOUR_OF_DAY, 23) | |
| set(Calendar.MINUTE, 59) | |
| set(Calendar.SECOND, 59) | |
| } | |
| } | |
| fun getDayCalendar(timeMillis: Long = 0): Calendar { | |
| return Calendar.getInstance().apply { | |
| if (timeMillis != 0L) { | |
| timeInMillis = timeMillis | |
| } | |
| set(Calendar.HOUR_OF_DAY, 0) | |
| set(Calendar.MINUTE, 0) | |
| set(Calendar.SECOND, 0) | |
| set(Calendar.MILLISECOND, 0) | |
| } | |
| } | |
| fun toCalendar(date: LocalDate): Calendar { | |
| return Calendar.getInstance().apply { | |
| set(Calendar.DAY_OF_MONTH, date.dayOfMonth) | |
| set(Calendar.MONTH, date.monthValue - 1) | |
| set(Calendar.YEAR, date.year) | |
| } | |
| } | |
| fun toCalendar(strDate: String, dateFormat: String): Calendar { | |
| val simpleDateFormat = SimpleDateFormat(dateFormat) | |
| val date = simpleDateFormat.parse(strDate) | |
| return Calendar.getInstance().apply { | |
| time = date | |
| } | |
| } | |
| fun toLocalDate(millis: Long): LocalDate { | |
| val instant = Instant.ofEpochMilli(millis) | |
| return instant.atZone(ZoneId.systemDefault()).toLocalDate() | |
| } | |
| fun getYearDifference(millis: Long): String { | |
| val day = toLocalDate(millis) | |
| val today = LocalDate.now() | |
| return when (val year = today.year - day.year) { | |
| 0 -> "Today" | |
| 1 -> "1 Year Ago" | |
| else -> "$year Years Ago" | |
| } | |
| } | |
| /** | |
| * Convert Date Formate | |
| */ | |
| fun getServerFormat(date: Calendar): String { | |
| val simpleFormat: SimpleDateFormat = SimpleDateFormat(yyyyMMddFormat) | |
| return simpleFormat.format(date.time) | |
| } | |
| fun getFormattedLeaveDuration(startDate: String, endDate: String): String { | |
| val startYear = format(yyyyMMddFormat, startDate, yyyyFormat) | |
| val endYear = format(yyyyMMddFormat, endDate, yyyyFormat) | |
| if (startYear == endYear) { | |
| val startTime = format(yyyyMMddFormat, startDate, MMMdFormat) | |
| val endTime = format(yyyyMMddFormat, endDate, MMMdyyyyFormat) | |
| return "$startTime to $endTime" | |
| } else { | |
| return "$startDate - $endDate" | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment