Skip to content Skip to sidebar Skip to footer

Turn String Date From Json To A Date Object With Moshi

with Gson you would do this Gson gson = new GsonBuilder() .setDateFormat('yyyy-MM-dd'T'HH:mm') .create(); and pass it to the retrofit builder and Gson woul

Solution 1:

If you’d like to use a standard ISO-8601/RFC 3339 date adapter (you probably do) then you can use the built-in adapter:

Moshi moshi = new Moshi.Builder()
    .add(Date.class, new Rfc3339DateJsonAdapter().nullSafe())
    .build();

JsonAdapter<Date> dateAdapter = moshi.adapter(Date.class);
assertThat(dateAdapter.fromJson("\"1985-04-12T23:20:50.52Z\""))
    .isEqualTo(newDate(1985, 4, 12, 23, 20, 50, 520, 0));

You’ll need this Maven dependency to make that work:

<dependency><groupId>com.squareup.moshi</groupId><artifactId>moshi-adapters</artifactId><version>1.5.0</version></dependency>

If you want to use a custom format (you probably don’t), there’s more code. Write a method that accepts a date and formats it to a string, and another method that accepts a string and parses it as a date.

Object customDateAdapter = newObject() {
  final DateFormat dateFormat;
  {
    dateFormat = newSimpleDateFormat("yyyy-MM-dd'T'HH:mm");
    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
  }

  @ToJson synchronized StringdateToJson(Date d) {
    return dateFormat.format(d);
  }

  @FromJson synchronized DatedateToJson(String s) throws ParseException {
    return dateFormat.parse(s);
  }
};

Moshi moshi = newMoshi.Builder()
    .add(customDateAdapter)
    .build();

JsonAdapter<Date> dateAdapter = moshi.adapter(Date.class);
assertThat(dateAdapter.fromJson("\"1985-04-12T23:20\""))
    .isEqualTo(newDate(1985, 4, 12, 23, 20, 0, 0, 0));

You need to remember to use synchronized because SimpleDateFormat is not thread-safe. Also you need to configure a time zone for the SimpleDateFormat.

Solution 2:

In kotlin you can extend JsonAdapter class and create your own adapter:

classCustomDateAdapter : JsonAdapter<Date>() {
    privateval dateFormat = SimpleDateFormat(SERVER_FORMAT, Locale.getDefault())

    @FromJsonoverridefunfromJson(reader: JsonReader): Date? {
        returntry {
            val dateAsString = reader.nextString()
            synchronized(dateFormat) {
                dateFormat.parse(dateAsString)
            }
        } catch (e: Exception) {
            null
        }
    }

    @ToJsonoverridefuntoJson(writer: JsonWriter, value: Date?) {
        if (value != null) {
            synchronized(dateFormat) {
                writer.value(value.toString())
            }
        }
    }

    companionobject {
        constval SERVER_FORMAT = ("yyyy-MM-dd'T'HH:mm") // define your server format here
    }
}

Then, in your Retrofit initialization you can set the adapter with Moshi.Builder by doing:

privateval moshiBuilder = Moshi.Builder().add(CustomDateAdapter()) // Your custom date adapter hereval service: ApiService by lazy {
            val loggingInterceptor = HttpLoggingInterceptor()
            loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY

            val httpClient = OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor)
                .build()

            val retrofit = Retrofit.Builder()
                .baseUrl(BuildConfig.API_URL)
                .client(httpClient)
                .addConverterFactory(MoshiConverterFactory.create(moshiBuilder.build())) // And don`t forget to add moshi class when creating MoshiConverterFactory 
                .addCallAdapterFactory(CoroutineCallAdapterFactory())
                .build()

            retrofit.create(ApiService::class.java)
        }

Post a Comment for "Turn String Date From Json To A Date Object With Moshi"