Add new date extensions

feature/top10
Aurelien Hubert 7 years ago
parent 98bd3b461d
commit 842d98279e
  1. 24
      app/src/main/java/net/pokeranalytics/android/util/extensions/DateExtension.kt

@ -79,6 +79,14 @@ fun Date.getDayNumber() : String {
fun Date.getShortDayName() : String { fun Date.getShortDayName() : String {
return SimpleDateFormat("EEE", Locale.getDefault()).format(this) 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 // Return the month & year of the date
fun Date.getMonthAndYear(): String { fun Date.getMonthAndYear(): String {
return SimpleDateFormat("MMMM yyyy", Locale.getDefault()).format(this).capitalize() return SimpleDateFormat("MMMM yyyy", Locale.getDefault()).format(this).capitalize()
@ -117,4 +125,20 @@ fun Date.endOfDay() : Date {
calendar.set(Calendar.SECOND, 59) calendar.set(Calendar.SECOND, 59)
calendar.set(Calendar.MILLISECOND, 999) calendar.set(Calendar.MILLISECOND, 999)
return calendar.time return calendar.time
}
// Return the date of the beginning of the current month
fun Date.startOfMonth() : Date {
val calendar = Calendar.getInstance()
calendar.time = this.startOfDay()
calendar.set(Calendar.DAY_OF_MONTH, 1)
return calendar.time
}
// Return the date of the beginning of the current year
fun Date.startOfYear() : Date {
val calendar = Calendar.getInstance()
calendar.time = this.startOfMonth()
calendar.set(Calendar.MONTH, 0)
return calendar.time
} }
Loading…
Cancel
Save