From b6b5d495636f14d776087d9e255e7d7528522734 Mon Sep 17 00:00:00 2001 From: Gurmeen Bindra Date: Thu, 17 Jul 2014 10:43:44 +0100 Subject: Use common function for date time in all classes Before this patch, some classes used their own private functions for date time functions. This patch makes them use the one from DateTime class. Test-information: junits pass Change-Id: I1330c55fbf65205516d6847e4655992ad817fbc4 diff --git a/src/com/isode/stroke/parser/payloadparsers/PubSubEventSubscriptionParser.java b/src/com/isode/stroke/parser/payloadparsers/PubSubEventSubscriptionParser.java index eb01301..dda8a71 100644 --- a/src/com/isode/stroke/parser/payloadparsers/PubSubEventSubscriptionParser.java +++ b/src/com/isode/stroke/parser/payloadparsers/PubSubEventSubscriptionParser.java @@ -9,15 +9,13 @@ package com.isode.stroke.parser.payloadparsers; +import com.isode.stroke.base.DateTime; +import com.isode.stroke.elements.PubSubEventSubscription; +import com.isode.stroke.jid.JID; import com.isode.stroke.parser.AttributeMap; import com.isode.stroke.parser.GenericPayloadParser; import com.isode.stroke.parser.PayloadParser; import com.isode.stroke.parser.PayloadParserFactoryCollection; -import com.isode.stroke.jid.JID; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import com.isode.stroke.elements.PubSubEventSubscription; public class PubSubEventSubscriptionParser extends GenericPayloadParser { public PubSubEventSubscriptionParser(PayloadParserFactoryCollection parsers) { @@ -48,7 +46,7 @@ public void handleStartElement(String element, String ns, AttributeMap attribute } attributeValue = attributes.getAttribute("expiry"); if (!attributeValue.isEmpty()) { - getPayloadInternal().setExpiry(stringToDate(attributeValue)); + getPayloadInternal().setExpiry(DateTime.stringToDate(attributeValue)); } } @@ -91,16 +89,6 @@ private static PubSubEventSubscription.SubscriptionType parseSubscriptionType(St } } -private static Date stringToDate(String date) { - String format = "YYYY-MM-ddThh:mm:ssZ"; - SimpleDateFormat parser = new SimpleDateFormat(format); - try { - return parser.parse(date); - } catch (ParseException e) { - return null; - } -} - PayloadParserFactoryCollection parsers_; int level_; PayloadParser currentPayloadParser_; diff --git a/src/com/isode/stroke/serializer/payloadserializers/MUCPayloadSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/MUCPayloadSerializer.java index d6e7a10..9a72cfa 100644 --- a/src/com/isode/stroke/serializer/payloadserializers/MUCPayloadSerializer.java +++ b/src/com/isode/stroke/serializer/payloadserializers/MUCPayloadSerializer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, Isode Limited, London, England. + * Copyright (c) 2012-2014, Isode Limited, London, England. * All rights reserved. */ /* @@ -8,11 +8,7 @@ */ package com.isode.stroke.serializer.payloadserializers; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.TimeZone; - +import com.isode.stroke.base.DateTime; import com.isode.stroke.elements.MUCPayload; import com.isode.stroke.serializer.GenericPayloadSerializer; import com.isode.stroke.serializer.xml.XMLElement; @@ -48,13 +44,7 @@ public class MUCPayloadSerializer extends GenericPayloadSerializer { history = true; } if (muc.getSince() != null) { - SimpleDateFormat dfm = new SimpleDateFormat("YYYY-MM-dd"); - SimpleDateFormat tfm = new SimpleDateFormat("hh:mm:ss"); - Date date = muc.getSince(); - - dfm.setTimeZone(TimeZone.getTimeZone("UTC")); - tfm.setTimeZone(TimeZone.getTimeZone("UTC")); - String sinceDateString = dfm.format(date) + "T" + tfm.format(date) + "Z"; + String sinceDateString = DateTime.dateToString(muc.getSince()); historyElement.setAttribute("since", sinceDateString); history = true; } diff --git a/src/com/isode/stroke/serializer/payloadserializers/PubSubEventSubscriptionSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/PubSubEventSubscriptionSerializer.java index d34bfb1..3118752 100644 --- a/src/com/isode/stroke/serializer/payloadserializers/PubSubEventSubscriptionSerializer.java +++ b/src/com/isode/stroke/serializer/payloadserializers/PubSubEventSubscriptionSerializer.java @@ -9,13 +9,11 @@ package com.isode.stroke.serializer.payloadserializers; +import com.isode.stroke.base.DateTime; +import com.isode.stroke.elements.PubSubEventSubscription; import com.isode.stroke.serializer.GenericPayloadSerializer; import com.isode.stroke.serializer.PayloadSerializerCollection; import com.isode.stroke.serializer.xml.XMLElement; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.TimeZone; -import com.isode.stroke.elements.PubSubEventSubscription; public class PubSubEventSubscriptionSerializer extends GenericPayloadSerializer { public PubSubEventSubscriptionSerializer(PayloadSerializerCollection serializers) { @@ -46,7 +44,7 @@ protected String serializePayload(PubSubEventSubscription payload) { } if(payload.getExpiry() != null) { - element.setAttribute("expiry", dateToString(payload.getExpiry())); + element.setAttribute("expiry", DateTime.dateToString(payload.getExpiry())); } return element.serialize(); @@ -62,14 +60,5 @@ private static String serializeSubscriptionType(PubSubEventSubscription.Subscrip return "undefined-subscriptiontype"; } -private static String dateToString(Date date) { - SimpleDateFormat dfm = new SimpleDateFormat("YYYY-MM-dd"); - SimpleDateFormat tfm = new SimpleDateFormat("hh:mm:ss"); - dfm.setTimeZone(TimeZone.getTimeZone("UTC")); - tfm.setTimeZone(TimeZone.getTimeZone("UTC")); - String sinceDateString = dfm.format(date) + "T" + tfm.format(date) + "Z"; - return sinceDateString; -} - PayloadSerializerCollection serializers_; } -- cgit v0.10.2-6-g49f6