1
1
package com.google.firebase.example.dataconnect.ui.components
2
2
3
+ import android.os.Build
3
4
import android.widget.Space
4
5
import androidx.compose.foundation.background
5
6
import androidx.compose.foundation.layout.Column
@@ -17,17 +18,17 @@ import androidx.compose.ui.semantics.text
17
18
import androidx.compose.ui.text.font.FontWeight
18
19
import androidx.compose.ui.tooling.preview.Preview
19
20
import androidx.compose.ui.unit.dp
21
+ import com.google.firebase.dataconnect.LocalDate
22
+ import com.google.firebase.dataconnect.toJavaLocalDate
20
23
import java.text.SimpleDateFormat
21
- import java.time.LocalDate
22
- import java.time.LocalDateTime
23
- import java.util.Date
24
+ import java.time.format.DateTimeFormatter
24
25
import java.util.Locale
25
26
26
27
27
28
@Composable
28
29
fun ReviewCard (
29
30
userName : String ,
30
- date : Date ,
31
+ date : LocalDate ,
31
32
rating : Double ,
32
33
text : String ,
33
34
movieName : String? = null
@@ -55,10 +56,23 @@ fun ReviewCard(
55
56
modifier = Modifier .padding(bottom = 8 .dp)
56
57
) {
57
58
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
+ },
62
76
style = MaterialTheme .typography.titleMedium
63
77
)
64
78
Spacer (modifier = Modifier .width(8 .dp))
0 commit comments