src/net/fortuna/ical4j/model/Time.java

Tue, 10 Feb 2015 18:12:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 10 Feb 2015 18:12:00 +0100
changeset 0
fb9019fb1bf7
permissions
-rw-r--r--

Import initial revisions of existing project AndroidCaldavSyncAdapater,
forked from upstream repository at 27e8a0f8495c92e0780d450bdf0c7cec77a03a55.

     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;
    34 import java.text.DateFormat;
    35 import java.text.ParseException;
    36 import java.text.SimpleDateFormat;
    37 import java.util.TimeZone;
    39 import net.fortuna.ical4j.util.Dates;
    40 import net.fortuna.ical4j.util.TimeZones;
    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 {
    52     private static final long serialVersionUID = -8401010870773304348L;
    54     private boolean utc = false;
    56     /**
    57      * FORM #1: LOCAL TIME.
    58      */
    59     private static final String DEFAULT_PATTERN = "HHmmss";
    61     /**
    62      * FORM #2: UTC TIME.
    63      */
    64     private static final String UTC_PATTERN = "HHmmss'Z'";
    66     /**
    67      * @param timezone a timezone for the instance
    68      */
    69     public Time(final TimeZone timezone) {
    70         this(timezone, TimeZones.isUtc(timezone));
    71     }
    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     }
    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     }
    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     }
   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     }
   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     }
   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     }
   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     }
   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     }
   153     /**
   154      * @return true if time is utc
   155      */
   156     public final boolean isUtc() {
   157         return utc;
   158     }
   159 }

mercurial