001 package ezvcard.util;
002
003 import java.text.DateFormat;
004 import java.text.SimpleDateFormat;
005 import java.util.regex.Pattern;
006
007 /**
008 * Copyright 2011 George El-Haddad. All rights reserved.
009 *
010 * Redistribution and use in source and binary forms, with or without modification, are
011 * permitted provided that the following conditions are met:
012 *
013 * 1. Redistributions of source code must retain the above copyright notice, this list of
014 * conditions and the following disclaimer.
015 *
016 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
017 * of conditions and the following disclaimer in the documentation and/or other materials
018 * provided with the distribution.
019 *
020 * THIS SOFTWARE IS PROVIDED BY GEORGE EL-HADDAD ''AS IS'' AND ANY EXPRESS OR IMPLIED
021 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
022 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GEORGE EL-HADDAD OR
023 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
024 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
025 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
027 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
028 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 *
030 * The views and conclusions contained in the software and documentation are those of the
031 * authors and should not be interpreted as representing official policies, either expressed
032 * or implied, of George El-Haddad.
033 */
034
035 /*
036 Copyright (c) 2013, Michael Angstadt
037 All rights reserved.
038
039 Redistribution and use in source and binary forms, with or without
040 modification, are permitted provided that the following conditions are met:
041
042 1. Redistributions of source code must retain the above copyright notice, this
043 list of conditions and the following disclaimer.
044 2. Redistributions in binary form must reproduce the above copyright notice,
045 this list of conditions and the following disclaimer in the documentation
046 and/or other materials provided with the distribution.
047
048 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
049 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
050 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
051 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
052 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
053 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
054 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
055 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
056 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
057 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
058
059 The views and conclusions contained in the software and documentation are those
060 of the authors and should not be interpreted as representing official policies,
061 either expressed or implied, of the FreeBSD Project.
062 */
063
064 /**
065 * Represents the various ISO8601 date/time formats that vCard dates can be
066 * represented as.
067 * @author George El-Haddad
068 * @author Michael Angstadt
069 */
070 public enum ISOFormat {
071 //@formatter:off
072 /**
073 * Example: 20120701
074 */
075 DATE_BASIC("\\d{8}","yyyyMMdd"),
076
077 /**
078 * Example: 2012-07-01
079 */
080 DATE_EXTENDED("\\d{4}-\\d{2}-\\d{2}", "yyyy-MM-dd"),
081
082 /**
083 * Example: 20120701T142110-0500
084 */
085 TIME_BASIC("\\d{8}T\\d{6}[-\\+]\\d{4}", "yyyyMMdd'T'HHmmssZ"),
086
087 /**
088 * Example: 2012-07-01T14:21:10-05:00
089 */
090 TIME_EXTENDED("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}[-\\+]\\d{2}:\\d{2}", "yyyy-MM-dd'T'HH:mm:ssZ"),
091
092 /**
093 * Example: 20120701T192110Z
094 */
095 UTC_TIME_BASIC("\\d{8}T\\d{6}Z", "yyyyMMdd'T'HHmmssZ", "yyyyMMdd'T'HHmmss'Z'"),
096
097 /**
098 * Example: 2012-07-01T19:21:10Z
099 */
100 UTC_TIME_EXTENDED("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z", "yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:ss'Z'"),
101
102 /**
103 * Example: 2012-07-01T14:21:10-0500
104 */
105 HCARD_TIME_TAG("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}[-\\+]\\d{2}:?\\d{2}", "yyyy-MM-dd'T'HH:mm:ssZ");
106 //@formatter:on
107
108 /**
109 * The regular expression pattern for the date format.
110 */
111 private final Pattern pattern;
112
113 /**
114 * The {@link SimpleDateFormat} format string used for parsing dates.
115 */
116 private final String parseFormat;
117
118 /**
119 * The {@link SimpleDateFormat} format string used for formatting dates.
120 */
121 private final String formatFormat;
122
123 /**
124 * @param regex the regular expression for the date format
125 * @param format the {@link SimpleDateFormat} format string used for parsing
126 * and formatting dates.
127 */
128 private ISOFormat(String regex, String format) {
129 this(regex, format, format);
130 }
131
132 /**
133 * @param regex the regular expression for the date format
134 * @param parseFormat the {@link SimpleDateFormat} format string used for
135 * parsing dates.
136 * @param formatFormat the {@link SimpleDateFormat} format string used for
137 * formatting dates.
138 */
139 private ISOFormat(String regex, String parseFormat, String formatFormat) {
140 pattern = Pattern.compile(regex);
141 this.parseFormat = parseFormat;
142 this.formatFormat = formatFormat;
143 }
144
145 /**
146 * Determines whether a date string is in this ISO format.
147 * @param dateStr the date string
148 * @return true if it matches the date format, false if not
149 */
150 public boolean matches(String dateStr) {
151 return pattern.matcher(dateStr).matches();
152 }
153
154 /**
155 * Builds a {@link DateFormat} object for parsing dates in this ISO format.
156 * @return the {@link DateFormat} object
157 */
158 public DateFormat getParseDateFormat() {
159 return new SimpleDateFormat(parseFormat);
160 }
161
162 /**
163 * Builds a {@link DateFormat} object for formatting dates in this ISO
164 * format.
165 * @return the {@link DateFormat} object
166 */
167 public DateFormat getFormatDateFormat() {
168 return new SimpleDateFormat(formatFormat);
169 }
170 }