|
|
|
|
@ -9,7 +9,7 @@ import java.util.* |
|
|
|
|
|
|
|
|
|
// Return a double representing the hour / minute of a date from a calendar |
|
|
|
|
fun Calendar.hourMinute(): Double { |
|
|
|
|
return (this.get(Calendar.HOUR_OF_DAY) + this.get(Calendar.MINUTE).toDouble()/60.0).roundOffDecimal() |
|
|
|
|
return (this.get(Calendar.HOUR_OF_DAY) + this.get(Calendar.MINUTE).toDouble() / 60.0).roundOffDecimal() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -18,6 +18,7 @@ fun Calendar.isSameMonth(calendar: Calendar): Boolean { |
|
|
|
|
return calendar.get(Calendar.YEAR) == this.get(Calendar.YEAR) && |
|
|
|
|
calendar.get(Calendar.MONTH) == this.get(Calendar.MONTH) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Return if the calendar dates are in the same day |
|
|
|
|
fun Calendar.isSameDay(calendar: Calendar): Boolean { |
|
|
|
|
return calendar.get(Calendar.YEAR) == this.get(Calendar.YEAR) && |
|
|
|
|
@ -31,14 +32,17 @@ fun Calendar.isSameDay(calendar: Calendar): Boolean { |
|
|
|
|
fun Date.shortDate(): String { |
|
|
|
|
return DateFormat.getDateInstance(DateFormat.SHORT).format(this) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Return a short string of the date |
|
|
|
|
fun Date.mediumDate(): String { |
|
|
|
|
return DateFormat.getDateInstance(DateFormat.MEDIUM).format(this) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Return a long string of the date |
|
|
|
|
fun Date.longDate(): String { |
|
|
|
|
return DateFormat.getDateInstance(DateFormat.LONG).format(this) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Return a short string of the date |
|
|
|
|
fun Date.fullDate(): String { |
|
|
|
|
return DateFormat.getDateInstance(DateFormat.FULL).format(this) |
|
|
|
|
@ -48,14 +52,17 @@ fun Date.fullDate(): String { |
|
|
|
|
fun Date.shortTime(): String { |
|
|
|
|
return DateFormat.getTimeInstance(DateFormat.SHORT).format(this) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Return a short string of the time |
|
|
|
|
fun Date.mediumTime(): String { |
|
|
|
|
return DateFormat.getTimeInstance(DateFormat.MEDIUM).format(this) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Return a long string of the time |
|
|
|
|
fun Date.longTime(): String { |
|
|
|
|
return DateFormat.getTimeInstance(DateFormat.LONG).format(this) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Return a short string of the time |
|
|
|
|
fun Date.fullTime(): String { |
|
|
|
|
return DateFormat.getTimeInstance(DateFormat.FULL).format(this) |
|
|
|
|
@ -65,41 +72,49 @@ fun Date.fullTime(): String { |
|
|
|
|
fun Date.shortDateTime(): String { |
|
|
|
|
return DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(this) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Return a medium string of the date & time |
|
|
|
|
fun Date.mediumDateTime(): String { |
|
|
|
|
return DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM).format(this) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Return a long string of the date & time |
|
|
|
|
fun Date.longDateTime(): String { |
|
|
|
|
return DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG).format(this) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Return the full string of the date & time |
|
|
|
|
fun Date.fullDateTime(): String { |
|
|
|
|
return DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(this) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Return the day number of the date |
|
|
|
|
fun Date.getDayNumber() : String { |
|
|
|
|
fun Date.getDayNumber(): String { |
|
|
|
|
return SimpleDateFormat("dd", Locale.getDefault()).format(this) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Return the 3 first letters of the date's day |
|
|
|
|
fun Date.getShortDayName() : String { |
|
|
|
|
fun Date.getShortDayName(): String { |
|
|
|
|
return SimpleDateFormat("EEE", Locale.getDefault()).format(this) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Return the month of the date |
|
|
|
|
fun Date.getDateMonth(): String { |
|
|
|
|
return SimpleDateFormat("MMMM", Locale.getDefault()).format(this).capitalize() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Return the year of the date |
|
|
|
|
fun Date.getDateYear(): String { |
|
|
|
|
return SimpleDateFormat("yyyy", Locale.getDefault()).format(this).capitalize() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Return the month & year of the date |
|
|
|
|
fun Date.getMonthAndYear(): String { |
|
|
|
|
return SimpleDateFormat("MMMM yyyy", Locale.getDefault()).format(this).capitalize() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Return the netDuration between two dates |
|
|
|
|
fun Date.getFormattedDuration(toDate: Date) : String { |
|
|
|
|
fun Date.getFormattedDuration(toDate: Date): String { |
|
|
|
|
val difference = (toDate.time - this.time).toInt() |
|
|
|
|
val hours = (difference / (1000 * 60 * 60)) |
|
|
|
|
val minutes = (difference / (1000 * 60)) % 60 |
|
|
|
|
@ -111,7 +126,7 @@ fun Date.getFormattedDuration(toDate: Date) : String { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Return the date of the beginning of the current date |
|
|
|
|
fun Date.startOfDay() : Date { |
|
|
|
|
fun Date.startOfDay(): Date { |
|
|
|
|
val calendar = Calendar.getInstance() |
|
|
|
|
calendar.time = this |
|
|
|
|
calendar.set(Calendar.HOUR_OF_DAY, 0) |
|
|
|
|
@ -122,7 +137,7 @@ fun Date.startOfDay() : Date { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Return the date of the end of the current date |
|
|
|
|
fun Date.endOfDay() : Date { |
|
|
|
|
fun Date.endOfDay(): Date { |
|
|
|
|
val calendar = Calendar.getInstance() |
|
|
|
|
calendar.time = this |
|
|
|
|
calendar.set(Calendar.HOUR_OF_DAY, 23) |
|
|
|
|
@ -133,7 +148,7 @@ fun Date.endOfDay() : Date { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Return the date of the beginning of the current month |
|
|
|
|
fun Date.startOfMonth() : Date { |
|
|
|
|
fun Date.startOfMonth(): Date { |
|
|
|
|
val calendar = Calendar.getInstance() |
|
|
|
|
calendar.time = this.startOfDay() |
|
|
|
|
calendar.set(Calendar.DAY_OF_MONTH, 1) |
|
|
|
|
@ -141,7 +156,7 @@ fun Date.startOfMonth() : Date { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Return the date of the beginning of the current year |
|
|
|
|
fun Date.startOfYear() : Date { |
|
|
|
|
fun Date.startOfYear(): Date { |
|
|
|
|
val calendar = Calendar.getInstance() |
|
|
|
|
calendar.time = this.startOfMonth() |
|
|
|
|
calendar.set(Calendar.MONTH, 0) |
|
|
|
|
@ -149,12 +164,12 @@ fun Date.startOfYear() : Date { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Return the number of seconds until the next minute |
|
|
|
|
fun Date.getNextMinuteInseconds() : Int { |
|
|
|
|
fun Date.getNextMinuteInseconds(): Int { |
|
|
|
|
return (getNextMinuteInMilliseconds() / 1000).toInt() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Return the number of milliseconds until the next minute |
|
|
|
|
fun Date.getNextMinuteInMilliseconds() : Long { |
|
|
|
|
fun Date.getNextMinuteInMilliseconds(): Long { |
|
|
|
|
val calendar = Calendar.getInstance() |
|
|
|
|
calendar.add(Calendar.MINUTE, 1) |
|
|
|
|
calendar.set(Calendar.SECOND, 0) |
|
|
|
|
@ -163,5 +178,12 @@ fun Date.getNextMinuteInMilliseconds() : Long { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun Date.setHourMinutes(value: String) { |
|
|
|
|
|
|
|
|
|
val calendar1 = Calendar.getInstance() |
|
|
|
|
calendar1.time = this |
|
|
|
|
val calendar2 = Calendar.getInstance().apply { time = SimpleDateFormat("HH:mm", Locale.getDefault()).parse(value) } |
|
|
|
|
calendar1.set(Calendar.HOUR_OF_DAY, calendar2.get(Calendar.HOUR_OF_DAY)) |
|
|
|
|
calendar1.set(Calendar.MINUTE, calendar2.get(Calendar.MINUTE)) |
|
|
|
|
calendar1.set(Calendar.SECOND, 0) |
|
|
|
|
calendar1.set(Calendar.MILLISECOND, 0) |
|
|
|
|
this.time = calendar1.time.time |
|
|
|
|
} |
|
|
|
|
|