Created
March 18, 2023 19:38
-
-
Save mobibob/4cfd7a3f287e4ebe6c6d357b645ddd4c to your computer and use it in GitHub Desktop.
Format and math using LocalDate, LocalTime and LocalDate time in Kotlin / Compose. Preview enabled.
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
package com.mobidawg.android.beta.studylocaldatetime | |
import android.os.Bundle | |
import androidx.activity.ComponentActivity | |
import androidx.activity.compose.setContent | |
import androidx.compose.foundation.background | |
import androidx.compose.foundation.border | |
import androidx.compose.foundation.layout.* | |
import androidx.compose.material3.MaterialTheme | |
import androidx.compose.material3.Surface | |
import androidx.compose.material3.Text | |
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.tooling.preview.Preview | |
import androidx.compose.ui.unit.dp | |
import com.mobidawg.android.beta.studylocaldatetime.ui.theme.StudyLocalDateTimeTheme | |
import java.time.LocalDate | |
import java.time.LocalDateTime | |
import java.time.LocalTime | |
import java.time.format.DateTimeFormatter | |
import java.util.* | |
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
StudyLocalDateTimeTheme { | |
// A surface container using the 'background' color from the theme | |
Surface( | |
modifier = Modifier | |
.fillMaxSize() | |
.padding(4.dp) | |
.border(width = 1.dp, color = Color.Red), | |
color = MaterialTheme.colorScheme.background, | |
) { | |
FleetingTime(modifier = Modifier.background(color = Color.White)) | |
} | |
} | |
} | |
} | |
} | |
@Composable | |
fun FleetingTime(modifier: Modifier) { | |
val dateFormatter = DateTimeFormatter.ofPattern("MMM dd, ''yy", Locale.US) | |
val dateFormatter2000 = DateTimeFormatter.ofPattern("MMM dd, yyyy", Locale.US) | |
val dateTimeFormatter = DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy 'at' h:mm a", Locale.US) | |
val dateParser = DateTimeFormatter.ofPattern( "MMM dd, yyyy", Locale.US) | |
val timeFormatterHoursMinutes = DateTimeFormatter.ofPattern("hh:mm +.s", Locale.US) | |
val dt: LocalDateTime = LocalDateTime.now() | |
Column( | |
modifier | |
.padding(8.dp) | |
.border(width = 1.dp, color = Color.Blue), | |
verticalArrangement = Arrangement.Center, | |
horizontalAlignment = Alignment.CenterHorizontally | |
) { | |
Text( | |
text = "Time is fleeting. Enjoy time with your family.", | |
modifier = Modifier.padding(bottom = 24.dp) | |
) | |
// | |
Text(text = LocalDateTime.now().toString()) | |
Text(LocalDateTime.now().format(dateFormatter)) | |
Text(LocalDateTime.now().toLocalDate().minusYears(66).format(dateFormatter)) | |
Text(LocalDateTime.now().plusDays(14L).plusMinutes(22L).toString()) | |
Text(LocalDateTime.now().plusDays(14L).plusMinutes(22L).format(dateFormatter)) | |
Text(LocalDateTime.now().plusDays(14L).plusMinutes(22L).format(timeFormatterHoursMinutes)) | |
Text(LocalDateTime.now().plusDays(14L).plusMinutes(22L).format(dateFormatter2000)) | |
Text(LocalDateTime.now().format(timeFormatterHoursMinutes)) | |
Text(LocalDateTime.now().toLocalTime().plusMinutes(45).format(timeFormatterHoursMinutes)) | |
Text(LocalDateTime.now().format(dateTimeFormatter)) | |
Spacer(modifier = Modifier.height(16.dp)) | |
Text(text = LocalDate.now().toString()) | |
localDateFromDatestamp(LocalDate.now().toEpochDay())?.let { Text(it.format(dateFormatter2000)) } | |
localDateFromDatestamp(LocalDate.now().toEpochDay() + 1000L)?.let { Text( it.format(dateFormatter)) } | |
localDateFromDatestamp(LocalDate.now().toEpochDay() + 100L)?.let { Text( it.format(dateFormatter)) } | |
localDateFromDatestamp(LocalDate.now().toEpochDay() + 10L)?.let { Text( it.format(dateFormatter)) } | |
localDateFromDatestamp(LocalDate.now().toEpochDay() + 1L)?.let { Text( it.format(dateFormatter)) } | |
localDateFromDatestamp(LocalDate.now().toEpochDay() + 2L)?.let { Text( it.format(dateFormatter)) } | |
localDateFromDatestamp(LocalDate.now().toEpochDay() + 3L)?.let { Text( it.format(dateFormatter)) } | |
localDateFromDatestamp(LocalDate.now().toEpochDay() + 5L)?.let { Text( it.format(dateFormatter)) } | |
localDateFromDatestamp(LocalDate.now().toEpochDay() + 8L)?.let { Text( it.format(dateFormatter)) } | |
localDateFromDatestamp(LocalDate.now().toEpochDay() + 13L)?.let { Text( it.format(dateFormatter)) } | |
// | |
Spacer(modifier = Modifier.height(16.dp)) | |
Text(text = LocalTime.now().toString()) | |
localTimeFromDatestamp(LocalTime.now().toSecondOfDay() + 1L)?.let { Text( it.format(timeFormatterHoursMinutes)) } | |
localTimeFromDatestamp(LocalTime.now().toSecondOfDay() + 2L)?.let { Text( it.format(timeFormatterHoursMinutes)) } | |
localTimeFromDatestamp(LocalTime.now().toSecondOfDay() + 3L)?.let { Text( it.format(timeFormatterHoursMinutes)) } | |
localTimeFromDatestamp(LocalTime.now().toSecondOfDay() + 5L)?.let { Text( it.format(timeFormatterHoursMinutes)) } | |
localTimeFromDatestamp(LocalTime.now().toSecondOfDay() + 8L)?.let { Text( it.format(timeFormatterHoursMinutes)) } | |
localTimeFromDatestamp(LocalTime.now().toSecondOfDay() + 13L)?.let { Text( it.format(timeFormatterHoursMinutes)) } | |
localTimeFromDatestamp(LocalTime.now().toSecondOfDay() + 60L)?.let { Text( it.format(timeFormatterHoursMinutes)) } | |
localTimeFromDatestamp(LocalTime.now().toSecondOfDay() + (60L * 60L))?.let { Text( it.format(timeFormatterHoursMinutes)) } | |
} | |
} | |
@Preview(showBackground = true) | |
@Composable | |
fun DefaultPreview() { | |
StudyLocalDateTimeTheme { | |
FleetingTime( | |
modifier = Modifier | |
.fillMaxSize() | |
.padding(6.dp) | |
.border(width = 1.dp, color = Color.Red) | |
) | |
} | |
} | |
fun localDateFromDatestamp(value: Long?): LocalDate? { | |
return value?.let { LocalDate.ofEpochDay(it) } | |
} | |
fun localTimeFromDatestamp(value: Long?): LocalTime? { | |
return value?.let { LocalTime.ofSecondOfDay(it) } | |
} |
Author
mobibob
commented
Mar 18, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment