diff --git a/src/main/java/microsoft/exchange/webservices/data/DateTimePropertyDefinition.java b/src/main/java/microsoft/exchange/webservices/data/DateTimePropertyDefinition.java index 715ab4b15..84d679306 100644 --- a/src/main/java/microsoft/exchange/webservices/data/DateTimePropertyDefinition.java +++ b/src/main/java/microsoft/exchange/webservices/data/DateTimePropertyDefinition.java @@ -10,6 +10,8 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of package microsoft.exchange.webservices.data; +import microsoft.exchange.webservices.data.util.DateTimeParser; + import java.util.Date; import java.util.EnumSet; @@ -71,12 +73,11 @@ protected DateTimePropertyDefinition(String xmlElementName, String uri, * @param propertyBag the property bag * @throws Exception the exception */ - protected void loadPropertyValueFromXml(EwsServiceXmlReader reader, - PropertyBag propertyBag) throws Exception { - String value = reader.readElementValue(XmlNamespace.Types, - getXmlElement()); - propertyBag.setObjectFromPropertyDefinition(this, reader.getService() - .convertUniversalDateTimeStringToDate(value)); + protected void loadPropertyValueFromXml(EwsServiceXmlReader reader, PropertyBag propertyBag) + throws Exception { + String value = reader.readElementValue(XmlNamespace.Types, getXmlElement()); + propertyBag.setObjectFromPropertyDefinition(this, new DateTimeParser() + .convertDateTimeStringToDate(value)); } diff --git a/src/main/java/microsoft/exchange/webservices/data/EwsServiceXmlReader.java b/src/main/java/microsoft/exchange/webservices/data/EwsServiceXmlReader.java index 3cd9c7d16..b9562f368 100644 --- a/src/main/java/microsoft/exchange/webservices/data/EwsServiceXmlReader.java +++ b/src/main/java/microsoft/exchange/webservices/data/EwsServiceXmlReader.java @@ -10,6 +10,8 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of package microsoft.exchange.webservices.data; +import microsoft.exchange.webservices.data.util.DateTimeParser; + import java.io.InputStream; import java.text.DateFormat; import java.text.ParseException; @@ -24,6 +26,8 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of */ class EwsServiceXmlReader extends EwsXmlReader { + private DateTimeParser dateTimeParser = new DateTimeParser(); + /** * The service. */ @@ -42,30 +46,6 @@ protected EwsServiceXmlReader(InputStream stream, ExchangeService service) this.service = service; } - /** - * Converts the specified string into a DateTime objects. - * - * @param dateTimeString The date time string to convert. - * @return A DateTime representing the converted string. - */ - private Date convertStringToDateTime(String dateTimeString) { - return this.service - .convertUniversalDateTimeStringToDate(dateTimeString); - } - - /** - * Converts the specified string into a - * unspecified Date object, ignoring offset. - * - * @param dateTimeString The date time string to convert. - * @return A DateTime representing the converted string. - * @throws java.text.ParseException - */ - private Date convertStringToUnspecifiedDate(String dateTimeString) throws ParseException { - return this.getService(). - convertStartDateToUnspecifiedDateTime(dateTimeString); - } - /** * Reads the element value as date time. * @@ -73,7 +53,7 @@ private Date convertStringToUnspecifiedDate(String dateTimeString) throws ParseE * @throws Exception the exception */ public Date readElementValueAsDateTime() throws Exception { - return this.convertStringToDateTime(this.readElementValue()); + return dateTimeParser.convertDateTimeStringToDate(readElementValue()); } /** @@ -83,7 +63,7 @@ public Date readElementValueAsDateTime() throws Exception { * @throws Exception */ public Date readElementValueAsUnspecifiedDate() throws Exception { - return this.convertStringToUnspecifiedDate(this.readElementValue()); + return dateTimeParser.convertDateStringToDate(readElementValue()); } /** @@ -144,10 +124,8 @@ public Date readElementValueAsUnbiasedDateTimeScopedToServiceTimeZone() * @return the date * @throws Exception the exception */ - public Date readElementValueAsDateTime(XmlNamespace xmlNamespace, - String localName) throws Exception { - return this.convertStringToDateTime(this.readElementValue(xmlNamespace, - localName)); + public Date readElementValueAsDateTime(XmlNamespace xmlNamespace, String localName) throws Exception { + return dateTimeParser.convertDateTimeStringToDate(readElementValue(xmlNamespace, localName)); } /** diff --git a/src/main/java/microsoft/exchange/webservices/data/ExchangeServiceBase.java b/src/main/java/microsoft/exchange/webservices/data/ExchangeServiceBase.java index 5b5dc5ed6..6c37330ff 100644 --- a/src/main/java/microsoft/exchange/webservices/data/ExchangeServiceBase.java +++ b/src/main/java/microsoft/exchange/webservices/data/ExchangeServiceBase.java @@ -396,127 +396,6 @@ private void traceHttpResponseHeaders(TraceFlags traceType, HttpWebRequest reque } } - /** - * Converts the universal date time string to local date time. - * - * @param dateString The value. - * @return DateTime Returned date is always in UTC date. - */ - protected Date convertUniversalDateTimeStringToDate(String dateString) { - String localTimeRegex = "^(.*)([+-]{1}\\d\\d:\\d\\d)$"; - Pattern localTimePattern = Pattern.compile(localTimeRegex); - String utcPattern = "yyyy-MM-dd'T'HH:mm:ss'Z'"; - String utcPattern1 = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'"; - String localPattern = "yyyy-MM-dd'T'HH:mm:ssz"; - String localPattern1 = "yyyy-MM-dd'Z'"; - String pattern = "yyyy-MM-ddz"; - String localPattern2 = "yyyy-MM-dd'T'HH:mm:ss"; - DateFormat utcFormatter = null; - Date dt = null; - String errMsg = String.format( - "Date String %s not in valid UTC/local format", dateString); - if (dateString == null || dateString.isEmpty()) { - return null; - } else { - if (dateString.endsWith("Z")) { - // String in UTC format yyyy-MM-ddTHH:mm:ssZ - utcFormatter = new SimpleDateFormat(utcPattern); - utcFormatter.setTimeZone(TimeZone.getTimeZone("UTC")); - try { - dt = utcFormatter.parse(dateString); - } catch (ParseException e) { - utcFormatter = new SimpleDateFormat(pattern); - utcFormatter.setTimeZone(TimeZone.getTimeZone("UTC")); - // dateString = dateString.substring(0, 10)+"T12:00:00Z"; - try { - dt = utcFormatter.parse(dateString); - } catch (ParseException e1) { - utcFormatter = new SimpleDateFormat(localPattern1); - utcFormatter.setTimeZone(TimeZone.getTimeZone("UTC")); - - try { - dt = utcFormatter.parse(dateString); - } catch (ParseException ex) { - - utcFormatter = new SimpleDateFormat(utcPattern1); - utcFormatter.setTimeZone(TimeZone.getTimeZone("UTC")); - } - try { - dt = utcFormatter.parse(dateString); - } catch (ParseException e2) { - throw new IllegalArgumentException(errMsg, e); - } - - } - // throw new IllegalArgumentException(errMsg,e); - } - } else if (dateString.endsWith("z")) { - // String in UTC format yyyy-MM-ddTHH:mm:ssZ - utcFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'z'"); - utcFormatter.setTimeZone(TimeZone.getTimeZone("UTC")); - try { - dt = utcFormatter.parse(dateString); - } catch (ParseException e) { - throw new IllegalArgumentException(e); - } - } else { - // dateString is not ending with Z. - // Check for yyyy-MM-ddTHH:mm:ss+/-HH:mm pattern - Matcher localTimeMatcher = localTimePattern.matcher(dateString); - if (localTimeMatcher.find()) { - System.out.println("Pattern matched"); - String date = localTimeMatcher.group(1); - String zone = localTimeMatcher.group(2); - // Add the string GMT between DateTime and TimeZone - // since 'z' in yyyy-MM-dd'T'HH:mm:ssz matches - // either format GMT+/-HH:mm or +/-HHmm - dateString = String.format("%sGMT%s", date, zone); - try { - utcFormatter = new SimpleDateFormat(localPattern); - utcFormatter.setTimeZone(TimeZone.getTimeZone("UTC")); - dt = utcFormatter.parse(dateString); - } catch (ParseException e) { - try { - utcFormatter = new SimpleDateFormat(pattern); - utcFormatter.setTimeZone(TimeZone.getTimeZone("UTC")); - dt = utcFormatter.parse(dateString); - } catch (ParseException ex) { - throw new IllegalArgumentException(ex); - } - } - } else { - // Invalid format - utcFormatter = new SimpleDateFormat(localPattern2); - utcFormatter.setTimeZone(TimeZone.getTimeZone("UTC")); - try { - dt = utcFormatter.parse(dateString); - } catch (ParseException e) { - e.printStackTrace(); - throw new IllegalArgumentException(errMsg); - } - } - } - return dt; - } - } - - /** - * Converts xs:dateTime string with either "Z", "-00:00" bias, or "" - * suffixes to unspecified StartDate value ignoring the suffix.Needs to fix - * E14:232996. - * - * @param value The string value to parse. - * @return The parsed DateTime value. - */ - protected Date convertStartDateToUnspecifiedDateTime(String value) throws ParseException { - if (value == null || value.isEmpty()) { - return null; - } else { - DateFormat df = new SimpleDateFormat("yyyy-MM-dd'Z'"); - return df.parse(value); - } - } - /** * Converts the date time to universal date time string. * diff --git a/src/main/java/microsoft/exchange/webservices/data/UserConfigurationDictionary.java b/src/main/java/microsoft/exchange/webservices/data/UserConfigurationDictionary.java index 0b65bc03e..e2603e2ed 100644 --- a/src/main/java/microsoft/exchange/webservices/data/UserConfigurationDictionary.java +++ b/src/main/java/microsoft/exchange/webservices/data/UserConfigurationDictionary.java @@ -10,6 +10,8 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of package microsoft.exchange.webservices.data; +import microsoft.exchange.webservices.data.util.DateTimeParser; + import javax.xml.stream.XMLStreamException; import java.lang.reflect.Array; import java.util.*; @@ -581,8 +583,7 @@ private Object constructObject(UserConfigurationDictionaryObjectType type, } else if (type.equals(UserConfigurationDictionaryObjectType.ByteArray)) { dictionaryObject = Base64EncoderStream.decode(value.get(0)); } else if (type.equals(UserConfigurationDictionaryObjectType.DateTime)) { - Date dateTime = reader.getService() - .convertUniversalDateTimeStringToDate(value.get(0)); + Date dateTime = new DateTimeParser().convertDateTimeStringToDate(value.get(0)); if (dateTime != null) { dictionaryObject = dateTime; } else { diff --git a/src/main/java/microsoft/exchange/webservices/data/util/DateTimeParser.java b/src/main/java/microsoft/exchange/webservices/data/util/DateTimeParser.java new file mode 100644 index 000000000..6bbbb3484 --- /dev/null +++ b/src/main/java/microsoft/exchange/webservices/data/util/DateTimeParser.java @@ -0,0 +1,101 @@ +/************************************************************************** + Exchange Web Services Java API + Copyright (c) Microsoft Corporation + All rights reserved. + MIT License + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + **************************************************************************/ + +package microsoft.exchange.webservices.data.util; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +public class DateTimeParser { + + private final SimpleDateFormat[] dateTimeFormats = { + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX"), + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX"), + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"), + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"), + new SimpleDateFormat("yyyy-MM-ddX"), + new SimpleDateFormat("yyyy-MM-dd") + }; + + private final SimpleDateFormat[] dateFormats = { + new SimpleDateFormat("yyyy-MM-ddX"), + new SimpleDateFormat("yyyy-MM-dd") + }; + + + public DateTimeParser() { + // Set default timezone of the formats to UTC, which will be used when the date string doesn't supply a + // timezone itself. + + for (SimpleDateFormat format : dateTimeFormats) { + format.setTimeZone(TimeZone.getTimeZone("UTC")); + } + + for (SimpleDateFormat format : dateFormats) { + format.setTimeZone(TimeZone.getTimeZone("UTC")); + } + } + + /** + * Converts a date time string to local date time. + * + * Note: this method also allows dates without times, in which case the time will be 00:00:00 in the + * supplied timezone. UTC timezone will be assumed if no timezone is supplied. + * + * @param value The string value to parse. + * @return The parsed {@link Date}. + * + * @throws java.lang.IllegalArgumentException If string can not be parsed. + */ + public Date convertDateTimeStringToDate(String value) { + return parseInternal(value, false); + } + + /** + * Converts a date string to local date time. + * + * UTC timezone will be assumed if no timezone is supplied. + * + * @param value The string value to parse. + * @return The parsed {@link Date}. + * + * @throws java.lang.IllegalArgumentException If string can not be parsed. + */ + public Date convertDateStringToDate(String value) { + return parseInternal(value, true); + } + + private Date parseInternal(String value, boolean dateOnly) { + String originalValue = value; + + if (value == null || value.isEmpty()) { + return null; + } else { + if (value.endsWith("z")) { + // This seems to be an edge case. Let's uppercase the Z to be sure. + value = value.substring(0, value.length() - 1) + "Z"; + } + + SimpleDateFormat[] formats = dateOnly ? dateFormats : dateTimeFormats; + for (SimpleDateFormat format : formats) { + try { + return format.parse(value); + } catch (ParseException e) { + // Ignore and try the next pattern. + } + } + } + + throw new IllegalArgumentException( + String.format("Date String %s not in valid UTC/local format", originalValue)); + } +} diff --git a/src/test/java/microsoft/exchange/webservices/data/util/DateTimeParserTest.java b/src/test/java/microsoft/exchange/webservices/data/util/DateTimeParserTest.java new file mode 100644 index 000000000..cd273f278 --- /dev/null +++ b/src/test/java/microsoft/exchange/webservices/data/util/DateTimeParserTest.java @@ -0,0 +1,219 @@ +/************************************************************************** + Exchange Web Services Java API + Copyright (c) Microsoft Corporation + All rights reserved. + MIT License + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + **************************************************************************/ + +package microsoft.exchange.webservices.data.util; + +import microsoft.exchange.webservices.data.util.DateTimeParser; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.TimeZone; + +import static org.junit.Assert.assertEquals; + +@RunWith(JUnit4.class) +public class DateTimeParserTest { + + private DateTimeParser parser; + + @Before + public void setUp() { + parser = new DateTimeParser(); + } + + + + // Tests for DateTimeParser.convertDateTimeStringToDate() + + @Test + public void testDateTimeZulu() { + String dateString = "2015-01-08T10:11:12Z"; + Date parsed = parser.convertDateTimeStringToDate(dateString); + Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); + calendar.setTime(parsed); + assertEquals(2015, calendar.get(Calendar.YEAR)); + assertEquals(0, calendar.get(Calendar.MONTH)); + assertEquals(8, calendar.get(Calendar.DATE)); + assertEquals(10, calendar.get(Calendar.HOUR_OF_DAY)); + assertEquals(11, calendar.get(Calendar.MINUTE)); + assertEquals(12, calendar.get(Calendar.SECOND)); + } + + @Test + public void testDateTimeZuluLowerZ() { + String dateString = "2015-01-08T10:11:12z"; + Date parsed = parser.convertDateTimeStringToDate(dateString); + Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); + calendar.setTime(parsed); + assertEquals(2015, calendar.get(Calendar.YEAR)); + assertEquals(0, calendar.get(Calendar.MONTH)); + assertEquals(8, calendar.get(Calendar.DATE)); + assertEquals(10, calendar.get(Calendar.HOUR_OF_DAY)); + assertEquals(11, calendar.get(Calendar.MINUTE)); + assertEquals(12, calendar.get(Calendar.SECOND)); + } + + @Test + public void testDateTimeZuluWithPrecision() { + String dateString = "2015-01-08T10:11:12.123Z"; + Date parsed = parser.convertDateTimeStringToDate(dateString); + Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); + calendar.setTime(parsed); + assertEquals(2015, calendar.get(Calendar.YEAR)); + assertEquals(0, calendar.get(Calendar.MONTH)); + assertEquals(8, calendar.get(Calendar.DATE)); + assertEquals(10, calendar.get(Calendar.HOUR_OF_DAY)); + assertEquals(11, calendar.get(Calendar.MINUTE)); + assertEquals(12, calendar.get(Calendar.SECOND)); + } + + @Test + public void testDateTimeWithTimeZone() { + String dateString = "2015-01-08T10:11:12+0200"; + Date parsed = parser.convertDateTimeStringToDate(dateString); + Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); + calendar.setTime(parsed); + assertEquals(2015, calendar.get(Calendar.YEAR)); + assertEquals(0, calendar.get(Calendar.MONTH)); + assertEquals(8, calendar.get(Calendar.DATE)); + assertEquals(8, calendar.get(Calendar.HOUR)); + assertEquals(11, calendar.get(Calendar.MINUTE)); + assertEquals(12, calendar.get(Calendar.SECOND)); + } + + @Test + public void testDateTimeWithTimeZoneWithColon() { + String dateString = "2015-01-08T10:11:12-02:00"; + Date parsed = parser.convertDateTimeStringToDate(dateString); + Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); + calendar.setTime(parsed); + assertEquals(2015, calendar.get(Calendar.YEAR)); + assertEquals(0, calendar.get(Calendar.MONTH)); + assertEquals(8, calendar.get(Calendar.DATE)); + assertEquals(12, calendar.get(Calendar.HOUR_OF_DAY)); + assertEquals(11, calendar.get(Calendar.MINUTE)); + assertEquals(12, calendar.get(Calendar.SECOND)); + } + + @Test + public void testDateTime() { + String dateString = "2015-01-08T10:11:12"; + Date parsed = parser.convertDateTimeStringToDate(dateString); + Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); + calendar.setTime(parsed); + assertEquals(2015, calendar.get(Calendar.YEAR)); + assertEquals(0, calendar.get(Calendar.MONTH)); + assertEquals(8, calendar.get(Calendar.DATE)); + assertEquals(10, calendar.get(Calendar.HOUR_OF_DAY)); + assertEquals(11, calendar.get(Calendar.MINUTE)); + assertEquals(12, calendar.get(Calendar.SECOND)); + } + + @Test + public void testDateZulu() { + String dateString = "2015-01-08Z"; + Date parsed = parser.convertDateTimeStringToDate(dateString); + Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); + calendar.setTime(parsed); + assertEquals(2015, calendar.get(Calendar.YEAR)); + assertEquals(0, calendar.get(Calendar.MONTH)); + assertEquals(8, calendar.get(Calendar.DATE)); + } + + @Test + public void testDateOnly() { + String dateString = "2015-01-08"; + Date parsed = parser.convertDateTimeStringToDate(dateString); + Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); + calendar.setTime(parsed); + assertEquals(2015, calendar.get(Calendar.YEAR)); + assertEquals(0, calendar.get(Calendar.MONTH)); + assertEquals(8, calendar.get(Calendar.DATE)); + } + + + + // Tests for DateTimeParser.convertDateStringToDate() + + @Test + public void testDateOnlyZulu() { + String dateString = "2015-01-08Z"; + Date parsed = parser.convertDateStringToDate(dateString); + Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); + calendar.setTime(parsed); + assertEquals(2015, calendar.get(Calendar.YEAR)); + assertEquals(0, calendar.get(Calendar.MONTH)); + assertEquals(8, calendar.get(Calendar.DATE)); + assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY)); + assertEquals(0, calendar.get(Calendar.MINUTE)); + assertEquals(0, calendar.get(Calendar.SECOND)); + } + + @Test + public void testDateOnlyZuluWithLowerZ() { + String dateString = "2015-01-08z"; + Date parsed = parser.convertDateStringToDate(dateString); + Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); + calendar.setTime(parsed); + assertEquals(2015, calendar.get(Calendar.YEAR)); + assertEquals(0, calendar.get(Calendar.MONTH)); + assertEquals(8, calendar.get(Calendar.DATE)); + assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY)); + assertEquals(0, calendar.get(Calendar.MINUTE)); + assertEquals(0, calendar.get(Calendar.SECOND)); + } + + @Test + public void testDateOnlyWithTimeZone() { + String dateString = "2015-01-08+0200"; + Date parsed = parser.convertDateStringToDate(dateString); + Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); + calendar.setTime(parsed); + assertEquals(2015, calendar.get(Calendar.YEAR)); + assertEquals(0, calendar.get(Calendar.MONTH)); + assertEquals(7, calendar.get(Calendar.DATE)); + assertEquals(22, calendar.get(Calendar.HOUR_OF_DAY)); + assertEquals(0, calendar.get(Calendar.MINUTE)); + assertEquals(0, calendar.get(Calendar.SECOND)); + } + + @Test + public void testDateOnlyWithTimeZoneWithColon() { + String dateString = "2015-01-08-02:00"; + Date parsed = parser.convertDateStringToDate(dateString); + Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); + calendar.setTime(parsed); + assertEquals(2015, calendar.get(Calendar.YEAR)); + assertEquals(0, calendar.get(Calendar.MONTH)); + assertEquals(8, calendar.get(Calendar.DATE)); + assertEquals(2, calendar.get(Calendar.HOUR_OF_DAY)); + assertEquals(0, calendar.get(Calendar.MINUTE)); + assertEquals(0, calendar.get(Calendar.SECOND)); + } + + @Test + public void testDateOnlyWithoutTimeZone() { + String dateString = "2015-01-08"; + Date parsed = parser.convertDateStringToDate(dateString); + Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); + calendar.setTime(parsed); + assertEquals(2015, calendar.get(Calendar.YEAR)); + assertEquals(0, calendar.get(Calendar.MONTH)); + assertEquals(8, calendar.get(Calendar.DATE)); + assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY)); + assertEquals(0, calendar.get(Calendar.MINUTE)); + assertEquals(0, calendar.get(Calendar.SECOND)); + } +}