0

I am getting json response in date format 2022-03-25T00:00:00.000Z I want get day , month and year seperately and attach in text view in recycler view kotlin.

Zaid
  • 29
  • 1
  • 4
  • Do these two question answer yours if you combine them? [Illegal pattern character 'T' when parsing a date string to java.util.Date](https://stackoverflow.com/questions/2597083/illegal-pattern-character-t-when-parsing-a-date-string-to-java-util-date) and [How to get year, month, day, hours, minutes, seconds and milliseconds of the current moment in Java?](https://stackoverflow.com/questions/2654025/how-to-get-year-month-day-hours-minutes-seconds-and-milliseconds-of-the-cur) – Ole V.V. Apr 19 '22 at 16:15
  • You tagged your question simpledateformat, but consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. Use [desugaring](https://developer.android.com/studio/write/java8-support-table) in order to use [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). It is so much nicer to work with. – Ole V.V. Apr 19 '22 at 16:16

2 Answers2

0

Try this

convertFormatOfDate(
"2022-03-25T00:00:00.000Z",
                "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
                "MMMM dd, yyyy"  //You will get Month,Day and Year here ..
            )



fun convertFormatOfDate(
        dateString: String,
        currentFormat: String,
        desiredFormat: String
    ): String {
        val currentSdf = SimpleDateFormat(currentFormat, Locale.getDefault())
        val currentDate: Date? = currentSdf.parse(dateString)
        val desiredSdf = SimpleDateFormat(desiredFormat, Locale.getDefault())
        return desiredSdf.format(currentDate!!)
    }
Sandesh Khutal
  • 1,712
  • 1
  • 4
  • 17
0

Please check -I have added separated value you can use as per your requirements.

    val str = convertFormatOfDate(
                "2022-03-25T00:00:00.000Z",
                "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
                "MMMM, dd, yyyy"  //You will get Month,Day and Year here ..
            )
  //OPTION 1
            val out = str.split(",")
            println("Year = " + out[2].chars())
            println("Month = " + out[0].chars())
            println("Day = " + out[1].chars())
    //OPTION 2
            val f: DateTimeFormatter = DateTimeFormatter.ofPattern("MMMM, dd, yyyy")
            val ld: LocalDate = LocalDate.parse(str, f)
            val year = ld.year
            val month = ld.month
            val dayOfMonth = ld.dayOfMonth
    
            Log.e("year", year.toString())
            Log.e("month", month.toString())
            Log.e("dayOfMonth", dayOfMonth.toString())
    
        }
    
        private fun convertFormatOfDate(
            dateString: String,
            currentFormat: String,
            desiredFormat: String
        ): String {
            val currentSdf = SimpleDateFormat(currentFormat, Locale.getDefault())
            val currentDate: Date? = currentSdf.parse(dateString)
            val desiredSdf = SimpleDateFormat(desiredFormat, Locale.getDefault())
            return desiredSdf.format(currentDate!!)
        }
Sandesh Khutal
  • 1,712
  • 1
  • 4
  • 17