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