Created
February 13, 2025 11:30
-
-
Save Rohit-554/28314a204be45b040c90e757f7e5d791 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Get epoch time | |
| const val formatPattern = "yyyy-MM-dd'T'HH:mm:ss[.SSS]" | |
| @OptIn(FormatStringsInDatetimeFormats::class) | |
| private val EpochDateTimeFormatter = LocalDateTime.Format { | |
| byUnicodePattern(formatPattern) | |
| } | |
| fun getFormattedDate(timeStamp: Long): String { | |
| return EpochDateTimeFormatter.format( | |
| Instant.fromEpochMilliseconds(timeStamp) | |
| .toLocalDateTime(TimeZone.currentSystemDefault()) | |
| ) | |
| } | |
| // Format the epoch time | |
| fun formatIsoStringToCustomDate(isoString: String): String { | |
| val localDateTime = LocalDateTime.parse(isoString) | |
| val day = localDateTime.dayOfMonth | |
| val month = localDateTime.month.name.lowercase().replaceFirstChar { it.uppercase() }.take(3) | |
| val year = localDateTime.year | |
| // Return the formatted string | |
| return "$day $month $year" | |
| } | |
| ------------------------------------------------------------------------------------------------------------------------------- | |
| // Utilise the Extension Function | |
| @OptIn(ExperimentalMaterial3Api::class, FormatStringsInDatetimeFormats::class) | |
| fun Long.getFormattedDate(): String { | |
| val localDateTime = | |
| Instant. | |
| fromEpochMilliseconds(this) | |
| .toLocalDateTime(TimeZone.currentSystemDefault()) | |
| val day = | |
| localDateTime.dayOfMonth | |
| val month = | |
| localDateTime.month.name.lowercase(). | |
| replaceFirstChar { it.uppercase() }.take(3) | |
| val year = | |
| localDateTime.year | |
| return "$day $month $year" | |
| } | |
| //usage | |
| val endDateFormatted = endDatePickerState.selectedDateMillis?.getFormattedDate() | |
| //output 13 Feb 2025 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment