001/*
002 * The MIT License
003 * Copyright (c) 2012 Microsoft Corporation
004 *
005 * Permission is hereby granted, free of charge, to any person obtaining a copy
006 * of this software and associated documentation files (the "Software"), to deal
007 * in the Software without restriction, including without limitation the rights
008 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
009 * copies of the Software, and to permit persons to whom the Software is
010 * furnished to do so, subject to the following conditions:
011 *
012 * The above copyright notice and this permission notice shall be included in
013 * all copies or substantial portions of the Software.
014 *
015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
016 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
017 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
019 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
020 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
021 * THE SOFTWARE.
022 */
023
024package microsoft.exchange.webservices.data.misc;
025
026import microsoft.exchange.webservices.data.core.exception.misc.ArgumentException;
027
028import java.util.Calendar;
029import java.util.Date;
030
031/**
032 * Represents time.
033 */
034public final class Time {
035
036  /**
037   * The hours.
038   */
039  private int hours;
040
041  /**
042   * The minutes.
043   */
044  private int minutes;
045
046  /**
047   * The seconds.
048   */
049  private int seconds;
050
051  /**
052   * Initializes a new instance of Time.
053   */
054  protected Time() {
055  }
056
057  /**
058   * Initializes a new instance of Time.
059   *
060   * @param minutes The number of minutes since 12:00AM.
061   * @throws ArgumentException the argument exception
062   */
063
064  protected Time(int minutes) throws ArgumentException {
065    this();
066    if (minutes < 0 || minutes >= 1440) {
067      throw new ArgumentException(String.format("%s,%s", "minutes must be between 0 and 1439, inclusive.", "minutes"));
068    }
069
070    this.hours = minutes / 60;
071    this.minutes = minutes % 60;
072    this.seconds = 0;
073  }
074
075  /**
076   * Initializes a new instance of Time.
077   *
078   * @param dateTime the date time
079   * @throws ArgumentException the argument exception
080   */
081  public Time(Date dateTime) throws ArgumentException {
082    if (dateTime != null) {
083      Calendar cal = Calendar.getInstance();
084      cal.setTime(dateTime);
085      this.setHours(cal.get(Calendar.HOUR));
086      this.setMinutes(cal.get(Calendar.MINUTE));
087      this.setSeconds(cal.get(Calendar.SECOND));
088    }
089  }
090
091  /**
092   * Initializes a new instance of Time.
093   *
094   * @param hours   the hours
095   * @param minutes the minutes
096   * @param seconds the seconds
097   */
098  protected Time(int hours, int minutes, int seconds) {
099    this();
100    this.hours = hours;
101    this.minutes = minutes;
102    this.seconds = seconds;
103  }
104
105  /**
106   * Convert Time to XML Schema time.
107   *
108   * @return String in XML Schema time format
109   */
110
111  public String toXSTime() {
112    return String.format("%s,%s,%s,%s", "{0:00}:{1:00}:{2:00}",
113        this.getHours(), this
114            .getMinutes(), this.getSeconds());
115  }
116
117  /**
118   * Converts the time into a number of minutes since 12:00AM.
119   *
120   * @return The number of minutes since 12:00AM the time represents.
121   */
122
123  protected int convertToMinutes() {
124    return this.getMinutes() + (this.getHours() * 60);
125  }
126
127  /**
128   * Gets  the hours.
129   *
130   * @return the hours
131   */
132  protected int getHours() {
133    return this.hours;
134  }
135
136  /**
137   * sets the hours.
138   *
139   * @param value the new hours
140   * @throws ArgumentException the argument exception
141   */
142
143  protected void setHours(int value) throws ArgumentException {
144    if (value >= 0 && value < 24) {
145      this.hours = value;
146    } else {
147      throw new ArgumentException("Hour must be between 0 and 23.");
148    }
149  }
150
151  /**
152   * Gets the minutes.
153   *
154   * @return the minutes
155   */
156  protected int getMinutes() {
157    return this.minutes;
158  }
159
160  /**
161   * Sets the minutes.
162   *
163   * @param value the new minutes
164   * @throws ArgumentException the argument exception
165   */
166  protected void setMinutes(int value) throws ArgumentException {
167    if (value >= 0 && value < 60) {
168      this.minutes = value;
169    } else {
170      throw new ArgumentException("Minute must be between 0 and 59.");
171    }
172  }
173
174  /**
175   * Gets the seconds.
176   *
177   * @return the seconds
178   */
179  protected int getSeconds() {
180    return this.seconds;
181  }
182
183  /**
184   * Sets the seconds.
185   *
186   * @param value the new seconds
187   * @throws ArgumentException the argument exception
188   */
189  protected void setSeconds(int value) throws ArgumentException {
190    if (value >= 0 && value < 60) {
191      this.seconds = value;
192    } else {
193      throw new ArgumentException("Second must be between 0 and 59.");
194    }
195  }
196}