Home > Software engineering >  Convert Simple date from timezone date string android
Convert Simple date from timezone date string android

Time:03-29

Input date string "2021-06-18T19:30 05:00"

and required output: DD-MMM-yyyy, HH:mm with time zone (ex. 28- April-2022 | 20:000 (IST))

tried below code

 val input = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
        val output = SimpleDateFormat("DD-MMM-yyyy, HH:mm")

        var d: Date? = null
        try {
            d = input.parse("2021-06-18T19:30 07:00")
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        val formatted = output.format(d)
        Log.i("DATE", ""   formatted)

getting exception

java.text.ParseException: Unparseable date: "2021-06-18T19:30 07:00"

how can I convert timezone date to required output ?

Thank you

CodePudding user response:

tl;dr

In Java syntax, as I do not know Kotlin.

No need to specify a specific formatting pattern when your input complies with standard ISO 8601 format.

java.time.OffsetDateTime
.parse( "2021-06-18T19:30 07:00" )
.format(
    DateTimeFormatter
    .ofPattern( "dd-MMM-uuuu, HH:mm" )
    .withLocale( Locale.US ) 
)

Details

You are using terrible date-time classes that were years ago supplanted by the modern java.time classes defined in JSR 310.

Your input represents a date with time of day as seen in an offset of seven hours ahead of UTC. For such an input, use java.time.OffsetDateTime.

OffsetDateTime odt = OffsetDateTime.parse( "2021-06-18T19:30 07:00" ) ;

To generate text in a specific format, use DateTimeFormatter. Specify a Locale to determine the human language and cultural norms to use in localizing the name of month.

Generally I recommend letting java.time automatically localize by using DateTimeFormatter.ofLocalizedDateTime, instead of hard-coding a specific custom format.

All this has been covered many times already on Stack Overflow. Search to learn more.

CodePudding user response:

As Michael mentioned, the date you trying to parse doesn't have seconds or milliseconds. The following code should work:

val input = SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ")
val output = SimpleDateFormat("dd-MMM-yyyy, HH:mm")

var d: Date? = null
try {
    d = input.parse("2021-06-18T19:30 07:00")
} catch (e: ParseException) {
    e.printStackTrace()
}
val formatted = output.format(d)
Log.i("DATE", ""   formatted)

Alternative solution: (recommended)

You can take advantage of Local DateTime API introduced in java 8.

val formattedDate = ZonedDateTime.parse("2021-06-18T19:30 07:00")
    .format(DateTimeFormatter.ofPattern("dd-MMM-yyyy, HH:mm"))
Log.i("DATE", ""   formattedDate)

The above code should work if your app minSDK is 26 or above. Otherwise, you have to add following in you app level build.gradle file to make it work for android api below 26.

android {
    defaultConfig {
        // Required when setting minSdkVersion to 20 or lower
        multiDexEnabled true
    }

​   compileOptions {
​        // Flag to enable support for the new language APIs
​        coreLibraryDesugaringEnabled true
​        // Sets Java compatibility to Java 8
​        sourceCompatibility JavaVersion.VERSION_1_8
​        targetCompatibility JavaVersion.VERSION_1_8
​    }

}

dependencies {
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
}

Know more: https://developer.android.com/studio/write/java8-support#library-desugaring

  • Related