View Javadoc

1   /*
2    * $Id: Period.java [Apr 14, 2004]
3    *
4    * Copyright (c) 2004, Ben Fortuna
5    * All rights reserved.
6    *
7    * Redistribution and use in source and binary forms, with or without
8    * modification, are permitted provided that the following conditions
9    * are met:
10   *
11   * 	o Redistributions of source code must retain the above copyright
12   * notice, this list of conditions and the following disclaimer.
13   *
14   * 	o Redistributions in binary form must reproduce the above copyright
15   * notice, this list of conditions and the following disclaimer in the
16   * documentation and/or other materials provided with the distribution.
17   *
18   * 	o Neither the name of Ben Fortuna nor the names of any other contributors
19   * may be used to endorse or promote products derived from this software
20   * without specific prior written permission.
21   *
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
26   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   */
34  package net.fortuna.ical4j.model;
35  
36  import java.text.ParseException;
37  import java.util.Date;
38  
39  import net.fortuna.ical4j.util.DateTimeFormat;
40  import net.fortuna.ical4j.util.DurationFormat;
41  
42  /***
43   * Defines a period of time.
44   *
45   * @author benf
46   */
47  public class Period {
48  
49      private Date start;
50  
51      private Date end;
52  
53      private long duration;
54  
55      /***
56       * Constructor.
57       * @param aValue a string representation of a period
58       * @throws ParseException where the specified string is
59       * not a valid representation
60       */
61      public Period(final String aValue) throws ParseException {
62  
63          start = DateTimeFormat.getInstance().parse(aValue.substring(0,
64                  aValue.indexOf('/') - 1));
65  
66          // period may end in either a date-time or a duration..
67          try {
68  
69              end = DateTimeFormat.getInstance()
70                      .parse(aValue.substring(aValue.indexOf('/')));
71          }
72          catch (ParseException pe) {
73  
74              duration = DurationFormat.getInstance().parse(aValue);
75          }
76      }
77  
78      /***
79       * @return Returns the duration.
80       */
81      public final long getDuration() {
82          return duration;
83      }
84  
85      /***
86       * @return Returns the end.
87       */
88      public final Date getEnd() {
89          return end;
90      }
91  
92      /***
93       * @return Returns the start.
94       */
95      public final Date getStart() {
96          return start;
97      }
98  
99      /***
100      * @see java.lang.Object#toString()
101      */
102     public final String toString() {
103         StringBuffer b = new StringBuffer();
104 
105         b.append(DateTimeFormat.getInstance().format(start));
106         b.append('/');
107 
108         if (end != null) {
109             b.append(DateTimeFormat.getInstance().format(end));
110         }
111         else {
112             b.append(DurationFormat.getInstance().format(duration));
113         }
114 
115         return b.toString();
116     }
117 }