From bad2718b1b85d806221fce52c76fa1d388993396 Mon Sep 17 00:00:00 2001 From: Alexander Alexeev Date: Wed, 7 Dec 2016 18:53:37 +0700 Subject: singleton java 8 date formatters --- src/test/java/com/juick/json/MessageSerializer.java | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) (limited to 'src/test/java/com/juick/json') diff --git a/src/test/java/com/juick/json/MessageSerializer.java b/src/test/java/com/juick/json/MessageSerializer.java index b49c991a..7ed060dd 100644 --- a/src/test/java/com/juick/json/MessageSerializer.java +++ b/src/test/java/com/juick/json/MessageSerializer.java @@ -20,6 +20,7 @@ package com.juick.json; import com.juick.Message; import com.juick.Photo; import com.juick.Tag; +import com.juick.util.DateFormattersHolder; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -27,26 +28,18 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.TimeZone; import java.util.stream.Collectors; /** - * * @author Ugnich Anton */ public class MessageSerializer extends JSONSerializer { - private final static Logger LOGGER = LoggerFactory.getLogger(MessageSerializer.class); - + UserSerializer userSerializer = new UserSerializer(); PlaceSerializer placeSerializer = new PlaceSerializer(); - private final SimpleDateFormat df; - public MessageSerializer() { - df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - df.setTimeZone(TimeZone.getTimeZone("UTC")); } @Override @@ -66,7 +59,7 @@ public class MessageSerializer extends JSONSerializer { jmsg.setText(json.getString("body").replace(""", "\"")); jmsg.setUser(userSerializer.deserialize(json.getJSONObject("user"))); - jmsg.setDate(df.parse(json.getString("timestamp"))); + jmsg.setDate(DateFormattersHolder.getMessageFormatterInstance().parse(json.getString("timestamp"))); if (json.has("tags")) { JSONArray tags = json.getJSONArray("tags"); @@ -115,7 +108,7 @@ public class MessageSerializer extends JSONSerializer { json.put("body", msg.getText()); } if (msg.getDate() != null) { - json.put("timestamp", df.format(msg.getDate())); + json.put("timestamp", DateFormattersHolder.getMessageFormatterInstance().format(msg.getDate())); } if (msg.getUser() != null) { json.put("user", userSerializer.serialize(msg.getUser())); @@ -145,8 +138,4 @@ public class MessageSerializer extends JSONSerializer { return json; } - - public SimpleDateFormat getDf() { - return df; - } } -- cgit v1.2.3