From 842d98279e7c238a12cb8f2b7e74575f578e61b4 Mon Sep 17 00:00:00 2001 From: Aurelien Hubert Date: Tue, 16 Apr 2019 09:36:50 +0200 Subject: [PATCH] Add new date extensions --- .../android/util/extensions/DateExtension.kt | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/app/src/main/java/net/pokeranalytics/android/util/extensions/DateExtension.kt b/app/src/main/java/net/pokeranalytics/android/util/extensions/DateExtension.kt index 6a5a6d10..b60ba293 100644 --- a/app/src/main/java/net/pokeranalytics/android/util/extensions/DateExtension.kt +++ b/app/src/main/java/net/pokeranalytics/android/util/extensions/DateExtension.kt @@ -79,6 +79,14 @@ fun Date.getDayNumber() : 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() @@ -117,4 +125,20 @@ fun Date.endOfDay() : Date { calendar.set(Calendar.SECOND, 59) calendar.set(Calendar.MILLISECOND, 999) 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 } \ No newline at end of file