-
Notifications
You must be signed in to change notification settings - Fork 566
Rework date parsing #176
Rework date parsing #176
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason for throwing a different kind of exception here? IllegalArgumentException vs. ParseException? I generally prefer to use illegal argument kind of exceptions to reflect programming errors where exceptions based on runtime data would use ParseException (or FormatException in .NET). Thoughts?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The former method
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, I've just read about checked vs. runtime exceptions, I'm new to the concept of checked exceptions since it is unique to Java. However, I would have thought that parsing should be a checked exception to force the callers to handle the case where the string they are providing is not valid. This is not something that you want to bubble to the top of the stack. It is interesting however that SimpleDateFormat throws ParseException (checked) and long.ParseLong throws IllegalArgumentException (runtime). I would have thought there would be consistency there and both use checked, since I would expect parsing errors to be handled closest to where they are used. |
||
| String.format("Date String %s not in valid UTC/local format", originalValue)); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to expose this for consumers of the library? Should we make it package internal?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I placed this class in a subpackage (yes, the first subpackage in this library), so we can't make it package private if we keep it there. This shouldn't be a problem though. All major libraries or frameworks expose internal utility classes like this. Otherwise package organisation wouldn't be possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So if we change a utility method interface, we will consider this a contract change? Hence, prefer to mark them as deprecated unless we are working on a major version? Or should we have SomeNameSpace.Internal.* to reflect that these are internal packages that shouldn't be used by client apps?