android 如何在Kotlin中将字符串或时间戳中的日期格式化为我喜欢的格式?

xzv2uavs  于 2023-06-28  发布在  Android
关注(0)|答案(6)|浏览(138)

我从两个版本的天气API中获取日期数据。第一个是这样的字符串:2019-08-07 09:00:00像这样:1565209665我如何将其更改为只显示日期或日期和月份的名称?例如,8月的星期一。
我在几个配置中尝试了类似的东西,但它只在完整版本中工作。如果我cat了某个东西,它会抛出一个错误:

var date = list.get(position).dt_txt
    val formatter = DateTimeFormatterBuilder()
        .appendPattern("yyyy-MM-dd HH:mm:ss").toFormatter()
    formatter.parse(date)
btxsgosb

btxsgosb1#

第一种API格式:

val firstApiFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
val date = LocalDate.parse("2019-08-07 09:00:00" , firstApiFormat)

Log.d("parseTesting", date.dayOfWeek.toString()) // prints Wednesday
Log.d("parseTesting", date.month.toString()) // prints August

第二种API格式:

val secondApiFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'")
val timestamp = 1565209665.toLong() // timestamp in Long

val timestampAsDateString = java.time.format.DateTimeFormatter.ISO_INSTANT
            .format(java.time.Instant.ofEpochSecond(timestamp))

Log.d("parseTesting", timestampAsDateString) // prints 2019-08-07T20:27:45Z

val date = LocalDate.parse(timestampAsDateString, secondApiFormat)

Log.d("parseTesting", date.dayOfWeek.toString()) // prints Wednesday
Log.d("parseTesting", date.month.toString()) // prints August
cx6n0qe3

cx6n0qe32#

这真的很简单

val dateFormated = SimpleDateFormat("dd/MM/yyyy").format(trans.created_date.toDate())

我希望这对每个人都有效,感谢https://www.datetimeformatter.com/how-to-format-date-time-in-kotlin/

ercv8c1e

ercv8c1e3#

尝试以下代码获取dayOfWeek和月份名称

编码
至字符串日期
方法

fun getAbbreviatedFromDateTime(dateTime: String, dateFormat: String, field: String): String? {
    val input = SimpleDateFormat(dateFormat)
    val output = SimpleDateFormat(field)
    try {
        val getAbbreviate = input.parse(dateTime)    // parse input
        return output.format(getAbbreviate)    // format output
    } catch (e: ParseException) {
        e.printStackTrace()
    }

    return null
}

*如何使用

val monthName=getAbbreviatedFromDateTime("2019-08-07 09:00:00","yyyy-MM-dd HH:mm:ss","MMMM")
    println("monthName--"+monthName)

    val dayOfWeek=getAbbreviatedFromDateTime("2019-08-07 09:00:00","yyyy-MM-dd HH:mm:ss","EEEE")
    println("dayOfWeek--"+dayOfWeek)

到蒂米利斯
方法

fun convertStringToCalendar( timeMillis: Long) {
    //get calendar instance
    val calendarDate = Calendar.getInstance()
    calendarDate.timeInMillis = timeMillis
    val month=getAbbreviatedFromDateTime(calendarDate,"MMMM");
    val day=getAbbreviatedFromDateTime(calendarDate,"EEEE");
    Log.d("parseTesting", month)// prints August
    Log.d("parseTesting",day)// prints Wednesday
}

fun getAbbreviatedFromDateTime(dateTime: Calendar, field: String): String? {
    val output = SimpleDateFormat(field)
    try {
        return output.format(dateTime.time)    // format output
    } catch (e: Exception) {
        e.printStackTrace()
    }

    return null
}

使用

val timestamp = "1565209665".toLong()

    convertStringToCalendar(timestamp)
lfapxunr

lfapxunr4#

试试这个

val stringDate="2019-08-07 09:00:00"

val dateFormat_yyyyMMddHHmmss = SimpleDateFormat(
    "yyyy-MM-dd HH:mm:ss", Locale.ENGLISH
)
val date = dateFormat_yyyyMMddHHmmss.parse(stringDate)
val calendar = Calendar.getInstance()
calendar.setTime(date)

val dayOfWeekString = calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.ENGLISH)

输出:
dayOfWeekString : wednesday

val timeInMillis = 1565242471228
val calendar = Calendar.getInstance()
calendar.setTimeInMillis(timeInMillis)

val dayOfWeekString = calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.ENGLISH)
ar5n3qh5

ar5n3qh55#

val parsedDate: String? = if(monthOfYear < 10 && dayOfMonth > 10){
                            "${dayOfMonth}/0${monthOfYear + 1}/${year}"

                        } else if(dayOfMonth < 10 && monthOfYear > 10) {
                            "0${dayOfMonth}/${monthOfYear + 1}/${year}"

                        } else if(dayOfMonth < 10 && monthOfYear < 10){
                            "0${dayOfMonth}/0${monthOfYear + 1}/${year}"
                        }else{
                            "0${dayOfMonth}/${monthOfYear + 1}/${year}"

                        }

                        date?.text = parsedDate

我尝试了不同的东西,但在日期选择器,这对我在kotlin工程

epfja78i

epfja78i6#

示例:2023年6月23日21:40

private val dateTimeFormat: DateFormat =
    DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT)

fun getFormattedDateTime(): String = dateTimeFormat.format(Date().time)

相关问题