Add Date.setHourMinutes extension

dev
Aurelien Hubert 7 years ago
parent 1cb4a8cc21
commit 7ae9cacc0a
  1. 24
      app/src/main/java/net/pokeranalytics/android/util/extensions/DateExtension.kt

@ -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,34 +72,42 @@ 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 {
return SimpleDateFormat("dd", Locale.getDefault()).format(this)
}
// Return the 3 first letters of the date's day
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()
@ -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
}

Loading…
Cancel
Save