|
1 /** |
|
2 * Copyright (c) 2012, Ben Fortuna |
|
3 * All rights reserved. |
|
4 * |
|
5 * Redistribution and use in source and binary forms, with or without |
|
6 * modification, are permitted provided that the following conditions |
|
7 * are met: |
|
8 * |
|
9 * o Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * o Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * o Neither the name of Ben Fortuna nor the names of any other contributors |
|
17 * may be used to endorse or promote products derived from this software |
|
18 * without specific prior written permission. |
|
19 * |
|
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
31 */ |
|
32 package net.fortuna.ical4j.model; |
|
33 |
|
34 import java.text.DateFormat; |
|
35 import java.text.ParseException; |
|
36 import java.text.SimpleDateFormat; |
|
37 import java.util.TimeZone; |
|
38 |
|
39 import net.fortuna.ical4j.util.Dates; |
|
40 import net.fortuna.ical4j.util.TimeZones; |
|
41 |
|
42 /** |
|
43 * $Id$ |
|
44 * |
|
45 * Created on 30/06/2005 |
|
46 * |
|
47 * A type used to represent iCalendar time values. |
|
48 * @author Ben Fortuna |
|
49 */ |
|
50 public class Time extends Iso8601 { |
|
51 |
|
52 private static final long serialVersionUID = -8401010870773304348L; |
|
53 |
|
54 private boolean utc = false; |
|
55 |
|
56 /** |
|
57 * FORM #1: LOCAL TIME. |
|
58 */ |
|
59 private static final String DEFAULT_PATTERN = "HHmmss"; |
|
60 |
|
61 /** |
|
62 * FORM #2: UTC TIME. |
|
63 */ |
|
64 private static final String UTC_PATTERN = "HHmmss'Z'"; |
|
65 |
|
66 /** |
|
67 * @param timezone a timezone for the instance |
|
68 */ |
|
69 public Time(final TimeZone timezone) { |
|
70 this(timezone, TimeZones.isUtc(timezone)); |
|
71 } |
|
72 |
|
73 /** |
|
74 * @param timezone a timezone for the instance |
|
75 * @param utc indicates if the time is in UTC |
|
76 */ |
|
77 public Time(final TimeZone timezone, boolean utc) { |
|
78 super(utc ? UTC_PATTERN : DEFAULT_PATTERN, Dates.PRECISION_SECOND, timezone); |
|
79 getFormat().setTimeZone(timezone); |
|
80 this.utc = utc; |
|
81 } |
|
82 |
|
83 /** |
|
84 * @param time a time value in milliseconds from the epoch |
|
85 * @param timezone a timezone for the instance |
|
86 */ |
|
87 public Time(final long time, final TimeZone timezone) { |
|
88 this(time, timezone, TimeZones.isUtc(timezone)); |
|
89 } |
|
90 |
|
91 /** |
|
92 * @param time a time value in milliseconds from the epoch |
|
93 * @param timezone a timezone for the instance |
|
94 * @param utc indicates if the time is in UTC |
|
95 */ |
|
96 public Time(final long time, final TimeZone timezone, boolean utc) { |
|
97 super(time, (utc ? UTC_PATTERN : DEFAULT_PATTERN), Dates.PRECISION_SECOND, timezone); |
|
98 getFormat().setTimeZone(timezone); |
|
99 this.utc = utc; |
|
100 } |
|
101 |
|
102 /** |
|
103 * @param time a time value in milliseconds from the epoch |
|
104 * @param timezone a timezone for the instance |
|
105 */ |
|
106 public Time(final java.util.Date time, final TimeZone timezone) { |
|
107 this(time, timezone, TimeZones.isUtc(timezone)); |
|
108 } |
|
109 |
|
110 /** |
|
111 * @param time a time value as a Java date instance |
|
112 * @param timezone a timezone for the instance |
|
113 * @param utc indicates if the time is in UTC |
|
114 */ |
|
115 public Time(final java.util.Date time, final TimeZone timezone, boolean utc) { |
|
116 super(time.getTime(), (utc ? UTC_PATTERN : DEFAULT_PATTERN), Dates.PRECISION_SECOND, timezone); |
|
117 getFormat().setTimeZone(timezone); |
|
118 this.utc = utc; |
|
119 } |
|
120 |
|
121 /** |
|
122 * @param value |
|
123 * @param timezone |
|
124 * @throws ParseException where the specified value is not a valid time string |
|
125 */ |
|
126 public Time(String value, TimeZone timezone) throws ParseException { |
|
127 this(value, timezone, TimeZones.isUtc(timezone)); |
|
128 } |
|
129 |
|
130 /** |
|
131 * @param value |
|
132 * @param timezone |
|
133 * @param utc |
|
134 * @throws ParseException where the specified value is not a valid time string |
|
135 */ |
|
136 public Time(String value, TimeZone timezone, boolean utc) throws ParseException { |
|
137 this(parseDate(value, timezone), timezone, utc); |
|
138 } |
|
139 |
|
140 private static java.util.Date parseDate(String value, TimeZone timezone) throws ParseException { |
|
141 DateFormat df = new SimpleDateFormat(DEFAULT_PATTERN); |
|
142 df.setTimeZone(timezone); |
|
143 try { |
|
144 return df.parse(value); |
|
145 } |
|
146 catch (ParseException e) { |
|
147 df = new SimpleDateFormat(UTC_PATTERN); |
|
148 df.setTimeZone(timezone); |
|
149 } |
|
150 return df.parse(value); |
|
151 } |
|
152 |
|
153 /** |
|
154 * @return true if time is utc |
|
155 */ |
|
156 public final boolean isUtc() { |
|
157 return utc; |
|
158 } |
|
159 } |