diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 1fcae37..a678dc8 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -67,4 +67,5 @@ dependencies { androidTestImplementation(libs.androidx.ui.test.junit4) debugImplementation(libs.androidx.ui.tooling) debugImplementation(libs.androidx.ui.test.manifest) + // implementation ("com.github.02gaurav:DateExtension:v1.0.0") } \ No newline at end of file diff --git a/app/src/main/java/com/gaurav/dateextension/MainActivity.kt b/app/src/main/java/com/gaurav/dateextension/MainActivity.kt index 633d8c3..ff37f17 100644 --- a/app/src/main/java/com/gaurav/dateextension/MainActivity.kt +++ b/app/src/main/java/com/gaurav/dateextension/MainActivity.kt @@ -4,14 +4,30 @@ import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge +import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.tooling.preview.Preview import com.gaurav.date_extensions.changeToDateFormat +import com.gaurav.date_extensions.getDayWithSuffix +import com.gaurav.date_extensions.getHourMinFormatString +import com.gaurav.date_extensions.getTime +import com.gaurav.date_extensions.getTimeInString +import com.gaurav.date_extensions.isDateLastMinuteOrBefore +import com.gaurav.date_extensions.isDateThisWeek +import com.gaurav.date_extensions.isDateToday +import com.gaurav.date_extensions.isDateYesterday +import com.gaurav.date_extensions.isDateYesterdayOrBefore +import com.gaurav.date_extensions.isMoreThanSevenDays +import com.gaurav.date_extensions.parseRemainingTimeDifference +import com.gaurav.date_extensions.parseTimeDifference +import com.gaurav.date_extensions.parseTimeTo_dd_MMM +import com.gaurav.date_extensions.timeDayOfWeek import com.gaurav.dateextension.ui.theme.DateExtensionTheme class MainActivity : ComponentActivity() { @@ -33,12 +49,113 @@ class MainActivity : ComponentActivity() { @Composable fun Greeting(name: String, modifier: Modifier = Modifier) { - val currentTime = System.currentTimeMillis() + val currentTime = System.currentTimeMillis() - 1000*60*24 val currentTime1 = System.currentTimeMillis() - Text( - text = "Hello $name! ${currentTime1.changeToDateFormat("yyyy-MM-dd")}", - modifier = modifier - ) + val currentTime2 = System.currentTimeMillis()- 1000*60*24*12 + val currentTime3 = System.currentTimeMillis()- 1000*60*60*24*8 + LazyColumn { + item { + Text( + text = "Date format ${ + (1694157381L).changeToDateFormat( + true, + "yyyy-MM-dd HH:mm:ss" + ) + }", + modifier = modifier + ) + } + item { + Text( + text = "Time diff ${(currentTime1-currentTime).parseTimeDifference()}", + modifier = modifier + ) + } + item { + Text( + text = "Rem time diff ${(currentTime1-currentTime2).parseRemainingTimeDifference()}", + modifier = modifier + ) + } + item { + Text( + text = "Is today ${currentTime1.isDateToday()}", + modifier = modifier + ) + } + item { + Text( + text = "Is Yesterday ${currentTime2.isDateYesterday()}", + modifier = modifier + ) + } + item { + Text( + text = "Get Time ${currentTime1.getTime()}", + modifier = modifier + ) + } + item { + Text( + text = "Yesterday or before ${currentTime1.isDateYesterdayOrBefore()}", + modifier = modifier + ) + } + item { + Text( + text = "Last min or before ${currentTime1.isDateLastMinuteOrBefore()}", + modifier = modifier + ) + } + item { + Text( + text = "parseTimeTo_dd_MMM = ${(1694157381L).parseTimeTo_dd_MMM(true)}", + modifier = modifier + ) + } + item { + Text( + text = "timeDayOfWeek${currentTime1.timeDayOfWeek()}", + modifier = modifier + ) + } + item { + + Text( + text = "isMoreThanSevenDays ${currentTime3.isMoreThanSevenDays()}", + modifier = modifier + ) + } + item { + Text( + text = "isDateThisWeek ${currentTime2.isDateThisWeek()}", + modifier = modifier + ) + } + item { + Text( + text = "getTimeInString ${(127L).getTimeInString()}", + modifier = modifier + ) + } + item { + + Text( + text = "getHourMinFormatString ${currentTime1.getHourMinFormatString()}", + modifier = modifier + ) + } + item { + + Text( + text = "getDayWithSuffix ${currentTime3.getDayWithSuffix()}", + modifier = modifier + ) + } + + //parseTimeTo_dd_MMM + } + } @Preview(showBackground = true) diff --git a/date-extensions/src/main/java/com/gaurav/date_extensions/CustomModels.kt b/date-extensions/src/main/java/com/gaurav/date_extensions/CustomModels.kt new file mode 100644 index 0000000..6882396 --- /dev/null +++ b/date-extensions/src/main/java/com/gaurav/date_extensions/CustomModels.kt @@ -0,0 +1,3 @@ +package com.gaurav.date_extensions + +data class CustomDate(val days: Long, val hours: Long, val minutes: Long, val seconds: Long) diff --git a/date-extensions/src/main/java/com/gaurav/date_extensions/DateExtensions.kt b/date-extensions/src/main/java/com/gaurav/date_extensions/DateExtensions.kt index 368be89..d69d9ad 100644 --- a/date-extensions/src/main/java/com/gaurav/date_extensions/DateExtensions.kt +++ b/date-extensions/src/main/java/com/gaurav/date_extensions/DateExtensions.kt @@ -1,7 +1,248 @@ package com.gaurav.date_extensions -fun Long.changeToDateFormat(format:String = "yyyy-MM-dd HH:mm:ss"):String { - val date = java.util.Date(this) - val dateStr = java.text.SimpleDateFormat(format) +import java.text.SimpleDateFormat +import java.util.Calendar +import java.util.Locale + +private fun Long.changeToMilliSec(isEpochTime:Boolean):Long { + return if(isEpochTime) this*1000 else this +} + +fun Long.changeToDateFormat(isEpochTime:Boolean = false, + format: String = "yyyy-MM-dd HH:mm:ss"): String { + val date = java.util.Date(this.changeToMilliSec(isEpochTime)) + val dateStr = SimpleDateFormat(format) return dateStr.format(date) +} + +fun Long.getDayFromTimestamp(isEpochTime:Boolean = false): Int { + val cal = Calendar.getInstance() + cal.timeInMillis = this.changeToMilliSec(isEpochTime) + return cal[Calendar.DAY_OF_MONTH] +} + +fun getDayOfMonthSuffix(day: Int): String? { + return if (day >= 11 && day <= 13) { + "th" + } else when (day % 10) { + 1 -> "st" + 2 -> "nd" + 3 -> "rd" + else -> "th" + } +} + +fun Long.parseTimeTo_dd_MMM(isEpochTime:Boolean = false): String { + if (this == 0L) + return "" + + var format = "" + val milliSeconds = this.changeToMilliSec(isEpochTime) + try { + val pattern = "d'" + getDayOfMonthSuffix(milliSeconds.getDayFromTimestamp()) + "' MMM" + val formatter = SimpleDateFormat(pattern, Locale.getDefault()) + format = formatter.format(milliSeconds) + } catch (e: Exception) { + e.printStackTrace() + } + return format +} + +private fun getStringFormatted(str: String, vararg values: String): String { + val format = str.replace("%d", values[0]) + return format +} + +fun Long.parseRemainingTimeDifference(isEpochTime:Boolean = false): String { + val timeInMillis = this.changeToMilliSec(isEpochTime) + val seconds = timeInMillis / 1000 + if (seconds < 60) { + return "Latest" + } + val minutes = seconds / 60 + if (minutes < 60) { + return getStringFormatted( + "%d minutes", + java.lang.Long.toString(minutes) + ) + } + val hours = minutes / 60 + if (hours < 24) { + return getStringFormatted( + "%d hours", + java.lang.Long.toString(hours) + ) + } + val days = hours / 24 + if (days < 30) { + return getStringFormatted( + "%d days", + java.lang.Long.toString(days) + ) + } + val months = days / 30 + if (months < 12) { + return getStringFormatted( + "%d months", + java.lang.Long.toString(months) + ) + } + val years = months / 12 + return getStringFormatted( + "%d years", + java.lang.Long.toString(years) + ) +} + +fun Long.parseTimeDifference(isEpochTime:Boolean = false): String { + + val seconds = this.changeToMilliSec(isEpochTime) / 1000 + if (seconds < 60) { + return "Latest" + } + val minutes = seconds / 60 + if (minutes < 60) { + return getStringFormatted( + "%d minutes ago", + java.lang.Long.toString(minutes) + ) + } + val hours = minutes / 60 + if (hours < 24) { + return getStringFormatted( + "%d hours ago", + java.lang.Long.toString(hours) + ) + } + val days = hours / 24 + if (days < 30) { + return getStringFormatted( + "%d days ago", + java.lang.Long.toString(days) + ) + } + val months = days / 30 + if (months < 12) { + return getStringFormatted( + "%d months ago", + java.lang.Long.toString(months) + ) + } + val years = months / 12 + return getStringFormatted( + "%d years ago", + java.lang.Long.toString(years) + ) +} + +fun Long.isDateToday(isEpochTime:Boolean = false): Boolean { + val calendar = Calendar.getInstance() + calendar.timeInMillis = this.changeToMilliSec(isEpochTime) + val todayCalendar = Calendar.getInstance() + todayCalendar.timeInMillis = System.currentTimeMillis() + return ( + calendar.get(Calendar.YEAR) == todayCalendar.get(Calendar.YEAR) && + calendar.get(Calendar.MONTH) == todayCalendar.get(Calendar.MONTH) && + calendar.get(Calendar.DAY_OF_MONTH) == todayCalendar.get(Calendar.DAY_OF_MONTH) + ) +} + +fun Long.isDateYesterday(isEpochTime:Boolean = false): Boolean { + val calendar = Calendar.getInstance() + calendar.timeInMillis = this.changeToMilliSec(isEpochTime) + val todayCalendar = Calendar.getInstance() + todayCalendar.timeInMillis = System.currentTimeMillis() + + return ( + calendar.get(Calendar.YEAR) == todayCalendar.get(Calendar.YEAR) && + calendar.get(Calendar.MONTH) == todayCalendar.get(Calendar.MONTH) && + calendar.get(Calendar.DAY_OF_YEAR) == todayCalendar.get(Calendar.DAY_OF_YEAR) - 1 + ) +} + +fun Long.isDateYesterdayOrBefore(isEpochTime: Boolean= false): Boolean { + val calendar = Calendar.getInstance() + calendar.timeInMillis = this.changeToMilliSec(isEpochTime) + val todayCalendar = Calendar.getInstance() + todayCalendar.timeInMillis = System.currentTimeMillis() + + return ( + calendar.get(Calendar.YEAR) == todayCalendar.get(Calendar.YEAR) && + calendar.get(Calendar.MONTH) == todayCalendar.get(Calendar.MONTH) && + calendar.get(Calendar.DAY_OF_YEAR) < todayCalendar.get(Calendar.DAY_OF_YEAR) + ) +} + +fun Long.isDateLastMinuteOrBefore(isEpochTime: Boolean= false): Boolean { + val calendar = Calendar.getInstance() + calendar.timeInMillis = this.changeToMilliSec(isEpochTime) + val todayCalendar = Calendar.getInstance() + todayCalendar.timeInMillis = System.currentTimeMillis() + + return ( + calendar.get(Calendar.YEAR) == todayCalendar.get(Calendar.YEAR) && + calendar.get(Calendar.MONTH) == todayCalendar.get(Calendar.MONTH) && + calendar.get(Calendar.DAY_OF_YEAR) == todayCalendar.get(Calendar.DAY_OF_YEAR) && + calendar.get(Calendar.HOUR_OF_DAY) == todayCalendar.get(Calendar.HOUR_OF_DAY) && + calendar.get(Calendar.MINUTE) < todayCalendar.get(Calendar.MINUTE) + ) +} + +fun Long.timeDayOfWeek(isEpochTime: Boolean= false): String { + return SimpleDateFormat("EEEE hh:mm a", Locale.getDefault()).format(this.changeToMilliSec(isEpochTime)) +} + +fun Long.isMoreThanSevenDays(isEpochTime: Boolean= false): Boolean { + return getDiffDate(this.changeToMilliSec(isEpochTime), System.currentTimeMillis()).days > 7 +} + +private fun getDiffDate(startDateInMillis: Long, endDateInMillis: Long): CustomDate { + val diff = endDateInMillis - startDateInMillis + val seconds = diff / 1000 + val minutes = seconds / 60 + val hours = minutes / 60 + val days = hours / 24 + + return CustomDate(days, hours % 24, minutes % 60, seconds % 60) +} + +fun Long.isDateThisWeek(isEpochTime: Boolean= false): Boolean { + val currentCalendar = Calendar.getInstance() + val week = currentCalendar[Calendar.WEEK_OF_YEAR] + val year = currentCalendar[Calendar.YEAR] + val targetCalendar = Calendar.getInstance() + targetCalendar.timeInMillis = this.changeToMilliSec(isEpochTime) + val targetWeek = targetCalendar[Calendar.WEEK_OF_YEAR] + val targetYear = targetCalendar[Calendar.YEAR] + return week == targetWeek && year == targetYear +} + +fun Long.getTimeInString(isEpochTime: Boolean = false): String { + val minutes = this.changeToMilliSec(isEpochTime) / 60 + val seconds = this.changeToMilliSec(isEpochTime) % 60 + return "$minutes : $seconds" +} + +fun Long.getHourMinFormatString(isEpochTime: Boolean = false): String { + val minutes = this.changeToMilliSec(isEpochTime) / (1000 * 60) % 60 + val hours = this.changeToMilliSec(isEpochTime) / (1000 * 60 * 60) % 24 + return "${if (hours < 10) "0" else ""}$hours:${if (minutes < 10) "0" else ""}$minutes" +} + +// Sample date 12:50 P.M +fun Long.getTime(isEpochTime: Boolean = false): String { + return SimpleDateFormat("hh:mm a", Locale.ENGLISH).format(this.changeToMilliSec(isEpochTime)).toUpperCase() +} + +fun Long.getDayWithSuffix(isEpochTime: Boolean = false): String { + val calendar = Calendar.getInstance() + calendar.timeInMillis = this.changeToMilliSec(isEpochTime) + + val day = calendar[Calendar.DAY_OF_MONTH] + + val dayWithSuffix = "$day${getDayOfMonthSuffix(day)}" + + val formatter = SimpleDateFormat("MMMM yyyy", Locale.ENGLISH) + val formattedDate = formatter.format(this.changeToMilliSec(isEpochTime)) + return "$dayWithSuffix $formattedDate" } \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index e637cd8..506e50a 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -16,6 +16,9 @@ dependencyResolutionManagement { repositories { google() mavenCentral() + maven { + setUrl("https://jitpack.io") + } } }