简体   繁体   English

将 Retrofit 字符串值转换为日期 object

[英]Convert Retrofit String value to a Date object

I want to convert Retrofit String value to a Date object.我想将 Retrofit 字符串值转换为日期 object。

For example, I have data like this:例如,我有这样的数据:

date: "20.04.1995"

And this is my POJO class.这是我的 POJO class。

public class LocationItem {

    @SerializedName("time")
    @Expose
    public String time;

    public long getTime() {
        return dateFormatter(time);
    }

    public void setTime(String time) {
        this.time = time;
    }
}

I want to save it in my POJO class as Date type instead of String.我想将它作为日期类型而不是字符串保存在我的 POJO class 中。 Currently I'm converting inside the get() method.目前我正在 get() 方法中进行转换。 However, instead of doing this every time, I want to do fetch the data, convert it to Date and save it to POJO class.但是,我不想每次都这样做,而是要获取数据,将其转换为日期并将其保存到 POJO class。 What I want:我想要的是:

public class LocationItem {

    @SerializedName("time")
    @Expose
    public Date time;

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }
}

This is my Retrofit service;这是我的 Retrofit 服务;

public static Retrofit createClient() {
        String baseUrl;

        ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.COMPATIBLE_TLS)
                .supportsTlsExtensions(true)
                .tlsVersions(TlsVersion.TLS_1_2, TlsVersion.TLS_1_1, TlsVersion.TLS_1_0)
                .cipherSuites(
                        CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
                        CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
                        CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
                        CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
                        CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
                        CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
                        CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
                        CipherSuite.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA,
                        CipherSuite.TLS_ECDHE_RSA_WITH_RC4_128_SHA,
                        CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
                        CipherSuite.TLS_DHE_DSS_WITH_AES_128_CBC_SHA,
                        CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA)
                .build();


        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder()
                .addInterceptor(NetworkInterceptor.create())
                .addInterceptor(RequestInterceptor.create())
                .connectionSpecs(Collections.singletonList(spec))
                .readTimeout(apiTimeout, TimeUnit.SECONDS)
                .connectTimeout(apiTimeout, TimeUnit.SECONDS)
                .writeTimeout(apiTimeout, TimeUnit.SECONDS);

        if (BuildConfig.DEBUG) {
            HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
            httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
            httpClientBuilder.addInterceptor(httpLoggingInterceptor);
        }

        vHttpClient = httpClientBuilder.build();

        Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();

        return new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .client(vHttpClient)
                .build();
    }

What should I do?我应该怎么办? Thanks for your help!谢谢你的帮助!

You should create an instance of Gson, and set your time format via setDateFormat builder pattern.您应该创建一个 Gson 的实例,并通过 setDateFormat 构建器模式设置您的时间格式。 Afterwards pass it to GsonConverterFactory.create() as argument.然后将其作为参数传递给 GsonConverterFactory.create()。

Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();

Retrofit retrofitAdapter = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM