Home > Mobile >  How to parse date format in Android
How to parse date format in Android

Time:08-31

Hello Stackoverflow team, I am getting date and time from backend in this format 2022-03-12T18:15:30 01:00[Europe/Paris] Any idea how can we parse this.

I tried this :

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZZ[V]");

but getting an error:

**java.lang.IllegalArgumentException: Illegal pattern character 'V'**

CodePudding user response:

Use java.time classes to format the data

   val date = "2022-03-12T18:15:30 01:00[Europe/Paris]"
   //above data is standard ISO Format
   val formatter = DateTimeFormatter.ISO_ZONED_DATE_TIME
   val dateTime: ZonedDateTime= ZonedDateTime.parse(date, formatter)
   // format to any date format you require
   val formatter2: DateTimeFormatter = DateTimeFormatter.ofPattern("dd MMM yyyy")
   Log.e("Date Format ", dateTime.format(formatter2))

Output:

E/Date Format: 12 Mar 2022

All predefined Format can be found here - DateTimeFormatter
Note: java.time classes or Java8 only works in Android 8 and above, to use these classes for lower version you need to enable desugaring - Enable Desugaring

CodePudding user response:

I would suggest using https://www.joda.org/joda-time/. It's super useful and super straight forward.

  • Related