How to format in Kotlin date in string or timestamp to my preferred format?

beginner992

I'm getting date data from weather API in two versions. The first one is just string like this: 2019-08-07 09:00:00 and like this: 1565209665. How change it to the just name of the day or day and month? For example: Monday, August.

I tried something like this in few configurations but it works only in full version. If I cat something then it throws an error:

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

First API format:

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

Second API format:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to extract date from the string format timestamp?

How to change date format in timestamp

How to format this string to a date format?

How to convert multiple string of date formats to timestamp with timezone format

How do you format a timestamp string to a Date object in Java?

How to format string to date

R: Create preferred date format

Convert a timestamp to ISO format date string

How do I convert my date string to a custom date format?

How to convert a timestamp string containing date and time in this format, to a PHP DateTime format?

How to format my date to Default format

How to convert Timestamp into Date format in java

How to handle date and time to convert to TimeStamp format

How to convert TimeStamp to appropriate Date format?

How to convert Timestamp to Date format in DataFrame?

how to display json date in timestamp format in html

how to cast a timestamp into a date format in kdb

How to convert timestamp to date format in Angular?

Convert Date String to desired format in Kotlin

How to set date format of a date into Date but not String?

How to format string into date sqlite

How to format date string in java?

how to format string to date on flutter?

How to format this incoming timestamp String from server?

How to format a timestamp to string with fractional seconds in BigQuery?

How to convert different formats of date timestamp to the format of timestamp in hive table

How to convert date string to timestamp in Kotlin?

How to parse Unix timestamp to date string in Kotlin

What determines (how to configure) what format string is used by php PDO driver for values of date and timestamp fields?