Gson Postgres时间戳序列化

时间:2016-10-26 11:48:01

标签: java android postgresql gson

问题在于使用GSON的Postegres时间戳序列化,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.juliandrach.eatfit.MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:weightSum="1">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:layout_margin="10dp"
        android:layout_weight=".33"
        android:background="@drawable/profilbild" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dp"
        android:layout_marginTop="15dp"
        android:layout_weight=".33"
        android:text="Ernährungspläne"
        android:textAllCaps="true"
        android:textColor="@android:color/black"
        android:textSize="21sp" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dp"
        android:layout_marginTop="15dp"
        android:layout_weight=".33"
        android:text="Shop"
        android:textAllCaps="true"
        android:textColor="@android:color/black"
        android:textSize="25sp" />
</LinearLayout></RelativeLayout>

预期结果:

private static final GsonBuilder GSON_BASE = Converters
        .registerAll(new GsonBuilder().disableHtmlEscaping())
        .registerTypeAdapter(InfoTransfer.class, new InfoTransfer.Adapter())
        .setDateFormat(DateFormat.FULL, DateFormat.FULL) //Line added but it seems work for MySQL DB Timestamp
        .setPrettyPrinting()
        .create();
.
//inner class in InfoTransfer 
public static class Adapter implements JsonSerializer<InfoTransfer>{

    @Override
    public JsonElement serialize(InfoTransfer src, Type typeOfSrc, JsonSerializationContext context) {

        Gson gson= new Gson();
        JsonObject jsonObject= (JsonObject) gson.toJsonTree(src);

        return new JsonPrimitive(jsonObject.getAsString());
    }
}
.
.
.
Log.d("Result",GSON_BASE.toJson(data));

结果:

"created_at": "2016-10-13 18:18:51.64208+01"

所以有什么建议吗?

1 个答案:

答案 0 :(得分:1)

日期格式可以设置如下: -

setDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSSX")

示例: -

public static void main(String[] args) {

        String jsonString = "{\"customorId\":\"506\",\"joiningDate\":\"2016-10-26 19:49:17.290671+01\"}";

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


        //Deserialize
        Customer customer = gson.fromJson(jsonString, Customer.class);
        System.out.println(customer.toString());

        //Serialize
        Customer customerSerialize = new Customer();
        customerSerialize.setCustomorId("123");
        customerSerialize.setJoiningDate(new Date());

        System.out.println(gson.toJson(customerSerialize));


    }

客户类: -

public class Customer implements Serializable {
    private static final long serialVersionUID = 1100012615187080642L;

    private String customorId;

    private Date joiningDate;

}

示例输出: -

Customer [customorId=506, joiningDate=Wed Oct 26 19:54:07 BST 2016]
{
  "customorId": "123",
  "joiningDate": "2016-10-26 20:23:37.000811+01"
}