11package com.google.firebase.example.dataconnect.ui.components
22
3+ import android.os.Build
34import android.widget.Space
45import androidx.compose.foundation.background
56import androidx.compose.foundation.layout.Column
@@ -17,17 +18,17 @@ import androidx.compose.ui.semantics.text
1718import androidx.compose.ui.text.font.FontWeight
1819import androidx.compose.ui.tooling.preview.Preview
1920import androidx.compose.ui.unit.dp
21+ import com.google.firebase.dataconnect.LocalDate
22+ import com.google.firebase.dataconnect.toJavaLocalDate
2023import java.text.SimpleDateFormat
21- import java.time.LocalDate
22- import java.time.LocalDateTime
23- import java.util.Date
24+ import java.time.format.DateTimeFormatter
2425import java.util.Locale
2526
2627
2728@Composable
2829fun ReviewCard (
2930 userName : String ,
30- date : Date ,
31+ date : LocalDate ,
3132 rating : Double ,
3233 text : String ,
3334 movieName : String? = null
@@ -55,10 +56,23 @@ fun ReviewCard(
5556 modifier = Modifier .padding(bottom = 8 .dp)
5657 ) {
5758 Text (
58- text = SimpleDateFormat (
59- " dd MMM, yyyy" ,
60- Locale .getDefault()
61- ).format(date),
59+ text =
60+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
61+ val dateFormatter = DateTimeFormatter .ofPattern(" dd MMM, yyyy" , Locale .getDefault())
62+ date.toJavaLocalDate().format(dateFormatter)
63+ } else {
64+ val parseableDateString = date.run {
65+ val year = " $year " .padStart(4 , ' 0' )
66+ val month = " $month " .padStart(2 , ' 0' )
67+ val day = " $day " .padStart(2 , ' 0' )
68+ " $year -$month -$day "
69+ }
70+ val dateParser = SimpleDateFormat (" y-M-d" , Locale .US )
71+ val parsedDate = dateParser.parse(parseableDateString) ? :
72+ throw Exception (" INTERNAL ERROR: unparseable date string: $parseableDateString " )
73+ val dateFormatter = SimpleDateFormat (" dd MMM, yyyy" , Locale .getDefault())
74+ dateFormatter.format(parsedDate)
75+ },
6276 style = MaterialTheme .typography.titleMedium
6377 )
6478 Spacer (modifier = Modifier .width(8 .dp))
0 commit comments