0

2021-07-06T19:27:46.811+0530 -> Current value as string

I want to convert to 05/07/2021, 06:45 am this format

Thanks in advance

  • Does this answer your question? [Android SimpleDateFormat, how to use it?](https://stackoverflow.com/questions/9277747/android-simpledateformat-how-to-use-it) – Ticherhaz FreePalestine Jul 05 '21 at 14:44
  • 1
    @TicherhazFreePalestine consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. See if you either can use [desugaring](https://developer.android.com/studio/write/java8-support-table) or add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project, in order to use java.time, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Jul 06 '21 at 03:17
  • 1
    Do you want format `d MMM yyyy, hh:mm aaa` as your title says, or `05/07/2021, 06:45 am` as stated in the body text? They don’t agree. – Ole V.V. Jul 06 '21 at 03:19
  • What did your search bring up? Related questions include [Cannot parse String in ISO 8601 format, lacking colon in offset, to Java 8 Date](https://stackoverflow.com/questions/43360852/cannot-parse-string-in-iso-8601-format-lacking-colon-in-offset-to-java-8-date) and [Generic support for ISO 8601 format in Java 6](https://stackoverflow.com/questions/13040143/generic-support-for-iso-8601-format-in-java-6). [I downvoted because research must be done to ask a good question](http://idownvotedbecau.se/noresearch/). – Ole V.V. Jul 06 '21 at 03:22

3 Answers3

3

Use java.time:

import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter

fun main() {
    val input = "2021-07-06T19:27:46.811+0530"
    // define a DateTimeFormatter for parsing your input
    val parser = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSx")
    // and another one for formatting your output
    val formatter = DateTimeFormatter.ofPattern("dd/MM/uuuu, hh:mm a")
    // then parse the input to an OffsetDateTime using the parser
    val converted: String = OffsetDateTime.parse(input, parser)
                                          // and output the same time differently
                                          .format(formatter)
    // output the result
    println(converted)
}

This code's output is

06/07/2021, 07:27 PM

Ok, it's not exactly the value as the example showing your desired output, but I think it's all about formatting here. Adjusting the values would require a little more effort including a brief description of the desired behaviour.

deHaar
  • 17,687
  • 10
  • 38
  • 51
  • 2
    It’s a good answer and really the answer that one should prefer to use. I think the question was pretty clear about wanting the *format* given, not that *value*, so I think we’re done. – Ole V.V. Jul 06 '21 at 03:24
1

You can do it like this

Java:

SimpleDateFormat parserFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault());
SimpleDateFormat convertFormat = new SimpleDateFormat("dd/MM/yyyy, hh:mm a", Locale.getDefault());
Date date = null;
try {
    date = parserFormat.parse("2021-07-06T19:27:46.811+0530");
    if (date != null) {
        String formatedDate = convertFormat.format(date);
        Log.e("formatted date",formatedDate);
    }
} catch (ParseException e) {
    e.printStackTrace();
    Log.e("formatted date",e.getMessage());
}

Kotlin:

val parserFormat =
        SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault())
val convertFormat =
        SimpleDateFormat("dd/MM/yyyy, hh:mm a", Locale.getDefault())
var date: Date? = null
try {
     date = parserFormat.parse("2021-07-06T19:27:46.811+0530")
     if (date != null) {
        val formatedDate = convertFormat.format(date)
        Log.e("formatted date", formatedDate)
     }
} catch (e: ParseException) {
    e.printStackTrace()
    Log.e("formatted date", e.message!!)
}
0

You can try this:

//Formats the date according to the given pattern
    private fun dateFormat(date: Date, pattern: String = "yyyy-MM-dd"): String =
        SimpleDateFormat(pattern, Locale.US).format(date)

    

And if you have to:

//string to date convert
        fun convertStringDate(dateString: String): LocalDate {
            val format = DateTimeFormatter.ISO_DATE
            val tmpDate = LocalDate.parse("2019-12-10", format)
            var parsedDate: LocalDate = tmpDate
            try {
                parsedDate = LocalDate.parse(dateString, format)
            } catch (e: Exception) {
                e.printStackTrace()
            }
            return parsedDate
        }
Haris
  • 372
  • 3
  • 16