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

changeset 0
fb9019fb1bf7
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/net/fortuna/ical4j/model/Time.java	Tue Feb 10 18:12:00 2015 +0100
     1.3 @@ -0,0 +1,159 @@
     1.4 +/**
     1.5 + * Copyright (c) 2012, Ben Fortuna
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * Redistribution and use in source and binary forms, with or without
     1.9 + * modification, are permitted provided that the following conditions
    1.10 + * are met:
    1.11 + *
    1.12 + *  o Redistributions of source code must retain the above copyright
    1.13 + * notice, this list of conditions and the following disclaimer.
    1.14 + *
    1.15 + *  o Redistributions in binary form must reproduce the above copyright
    1.16 + * notice, this list of conditions and the following disclaimer in the
    1.17 + * documentation and/or other materials provided with the distribution.
    1.18 + *
    1.19 + *  o Neither the name of Ben Fortuna nor the names of any other contributors
    1.20 + * may be used to endorse or promote products derived from this software
    1.21 + * without specific prior written permission.
    1.22 + *
    1.23 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.24 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.25 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.26 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    1.27 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    1.28 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    1.29 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    1.30 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    1.31 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    1.32 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    1.33 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.34 + */
    1.35 +package net.fortuna.ical4j.model;
    1.36 +
    1.37 +import java.text.DateFormat;
    1.38 +import java.text.ParseException;
    1.39 +import java.text.SimpleDateFormat;
    1.40 +import java.util.TimeZone;
    1.41 +
    1.42 +import net.fortuna.ical4j.util.Dates;
    1.43 +import net.fortuna.ical4j.util.TimeZones;
    1.44 +
    1.45 +/**
    1.46 + * $Id$
    1.47 + *
    1.48 + * Created on 30/06/2005
    1.49 + *
    1.50 + * A type used to represent iCalendar time values.
    1.51 + * @author Ben Fortuna
    1.52 + */
    1.53 +public class Time extends Iso8601 {
    1.54 +    
    1.55 +    private static final long serialVersionUID = -8401010870773304348L;
    1.56 +    
    1.57 +    private boolean utc = false;
    1.58 +    
    1.59 +    /**
    1.60 +     * FORM #1: LOCAL TIME.
    1.61 +     */
    1.62 +    private static final String DEFAULT_PATTERN = "HHmmss";
    1.63 +    
    1.64 +    /**
    1.65 +     * FORM #2: UTC TIME.
    1.66 +     */
    1.67 +    private static final String UTC_PATTERN = "HHmmss'Z'";
    1.68 +
    1.69 +    /**
    1.70 +     * @param timezone a timezone for the instance
    1.71 +     */
    1.72 +    public Time(final TimeZone timezone) {
    1.73 +        this(timezone, TimeZones.isUtc(timezone));
    1.74 +    }
    1.75 +    
    1.76 +    /**
    1.77 +     * @param timezone a timezone for the instance
    1.78 +     * @param utc indicates if the time is in UTC
    1.79 +     */
    1.80 +    public Time(final TimeZone timezone, boolean utc) {
    1.81 +        super(utc ? UTC_PATTERN : DEFAULT_PATTERN, Dates.PRECISION_SECOND, timezone);
    1.82 +        getFormat().setTimeZone(timezone);
    1.83 +        this.utc = utc;
    1.84 +    }
    1.85 +
    1.86 +    /**
    1.87 +     * @param time a time value in milliseconds from the epoch
    1.88 +     * @param timezone a timezone for the instance
    1.89 +     */
    1.90 +    public Time(final long time, final TimeZone timezone) {
    1.91 +        this(time, timezone, TimeZones.isUtc(timezone));
    1.92 +    }
    1.93 +    
    1.94 +    /**
    1.95 +     * @param time a time value in milliseconds from the epoch
    1.96 +     * @param timezone a timezone for the instance
    1.97 +     * @param utc indicates if the time is in UTC
    1.98 +     */
    1.99 +    public Time(final long time, final TimeZone timezone, boolean utc) {
   1.100 +        super(time, (utc ? UTC_PATTERN : DEFAULT_PATTERN), Dates.PRECISION_SECOND, timezone);
   1.101 +        getFormat().setTimeZone(timezone);
   1.102 +        this.utc = utc;
   1.103 +    }
   1.104 +
   1.105 +    /**
   1.106 +     * @param time a time value in milliseconds from the epoch
   1.107 +     * @param timezone a timezone for the instance
   1.108 +     */
   1.109 +    public Time(final java.util.Date time, final TimeZone timezone) {
   1.110 +        this(time, timezone, TimeZones.isUtc(timezone));
   1.111 +    }
   1.112 +    
   1.113 +    /**
   1.114 +     * @param time a time value as a Java date instance
   1.115 +     * @param timezone a timezone for the instance
   1.116 +     * @param utc indicates if the time is in UTC
   1.117 +     */
   1.118 +    public Time(final java.util.Date time, final TimeZone timezone, boolean utc) {
   1.119 +        super(time.getTime(), (utc ? UTC_PATTERN : DEFAULT_PATTERN), Dates.PRECISION_SECOND, timezone);
   1.120 +        getFormat().setTimeZone(timezone);
   1.121 +        this.utc = utc;
   1.122 +    }
   1.123 +    
   1.124 +    /**
   1.125 +     * @param value
   1.126 +     * @param timezone
   1.127 +     * @throws ParseException where the specified value is not a valid time string
   1.128 +     */
   1.129 +    public Time(String value, TimeZone timezone) throws ParseException {
   1.130 +        this(value, timezone, TimeZones.isUtc(timezone));
   1.131 +    }
   1.132 +    
   1.133 +    /**
   1.134 +     * @param value
   1.135 +     * @param timezone
   1.136 +     * @param utc
   1.137 +     * @throws ParseException where the specified value is not a valid time string
   1.138 +     */
   1.139 +    public Time(String value, TimeZone timezone, boolean utc) throws ParseException {
   1.140 +        this(parseDate(value, timezone), timezone, utc);
   1.141 +    }
   1.142 +    
   1.143 +    private static java.util.Date parseDate(String value, TimeZone timezone) throws ParseException {
   1.144 +        DateFormat df = new SimpleDateFormat(DEFAULT_PATTERN);
   1.145 +        df.setTimeZone(timezone);
   1.146 +        try {
   1.147 +            return df.parse(value);
   1.148 +        }
   1.149 +        catch (ParseException e) {
   1.150 +            df = new SimpleDateFormat(UTC_PATTERN);
   1.151 +            df.setTimeZone(timezone);
   1.152 +        }
   1.153 +        return df.parse(value);
   1.154 +    }
   1.155 +    
   1.156 +    /**
   1.157 +     * @return true if time is utc
   1.158 +     */
   1.159 +    public final boolean isUtc() {
   1.160 +        return utc;
   1.161 +    }
   1.162 +}

mercurial