1

    fun bind(crime: Crime){
            this.crime = crime
            titleTextView.text = this.crime.title
            val df : DateFormat = DateFormat.getInstance().format(DateFormat.LONG, DateFormat.MEDIUM)
            dateTextView.text = df.format(this.crime.date.toString())
            solvedImageView.visibility = if (crime.isSolved){
                View.VISIBLE
            }else{
                View.GONE
            }
        }

How to use Date Format library with Kotlin?

I want to change this date format "Sun Jul 25 00:00:00 GMT+09:00 2021" to "Sun, Jul, 25, 2021".

GreenHelix
  • 51
  • 1
  • 7
  • 1
    Can you please print the result of ```val df : DateFormat = DateFormat.getInstance().format(DateFormat.LONG, DateFormat.MEDIUM) dateTextView.text = df.format(this.crime.date.toString())``` – Mohit Ajwani Jul 25 '21 at 13:40
  • @MohitAjwani Thank you . But, format() functions don't have this parameter. This comment showed me by android studio. --------------------------------------------------------- None of the following functions can be called with the arguments supplied. format(Date!) defined in java.text.DateFormat format(Date!, StringBuffer!, FieldPosition!) defined in java.text.DateFormat format(Any!) defined in java.text.DateFormat format(Any!, StringBuffer!, FieldPosition!) defined in java.text.DateFormat --------------------------------------------------------- – GreenHelix Jul 26 '21 at 08:00
  • https://www.geeksforgeeks.org/date-and-time-formatting-in-android/ – GreenHelix Jul 26 '21 at 08:36

3 Answers3

2

You can simply use the below:

val formattedDate = SimpleDateFormat("EE, MMM, dd, yyyy", Locale.US)
val date = formattedDate .parse(this.crime.date.toString())
Hascher
  • 486
  • 1
  • 3
  • 12
  • ```kotlin simpleDateFormat = SimpleDateFormat("EE, MMM, dd, yyyy", Locale.KOREA) val example : String = simpleDateFormat.format(this.crime.date).toString() dateTextView.text = example ``` This code is work! Thank you – GreenHelix Jul 26 '21 at 08:25
  • Great I am glad :) – Hascher Jul 26 '21 at 09:08
1

Check DateFormat and SimpleDateFormat classes from Java Docs https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

fun getTimeMillisFromString(dateValue: String, dateFormat: String): Long {
    val sdf = SimpleDateFormat(dateFormat)
    try {
        return sdf.parse(dateValue).time
    } catch (e: ParseException) {
        e.printStackTrace()
    }
}
return 0L

This function will return the long format of the time. You need to pass the string you received and the format in which the string is. For example, getTimeMillisFromString("Sun Jul 25 00:00:00 GMT+09:00 2021", "yyyy.MM.dd HH:mm:ss z")

You may have to set timezone as GMT like sdf.setTimeZone(TimeZone.getTimeZone("GMT"))

After you have the long format date, you can convert it into any string format you want using SimpleDateFormat. For example,

SimpleDateFormat(DATE_FORMAT_HYPHEN_DD_MM_YY, Locale.getDefault()).format(calender.time)

This should give you the expected string.

Check this answer also SimpleDateFormat parse loses timezone

And this one also for more info Java SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") gives timezone as IST

Mohit Ajwani
  • 1,328
  • 12
  • 24
1

You can use the Calendar instance, then pass DAY_OF_WEEK YEAR MONTH

oriohac
  • 187
  • 1
  • 8