| 1 | // ================================================================================================= |
| 2 | // ADOBE SYSTEMS INCORPORATED |
| 3 | // Copyright 2006 Adobe Systems Incorporated |
| 4 | // All Rights Reserved |
| 5 | // |
| 6 | // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms |
| 7 | // of the Adobe license agreement accompanying it. |
| 8 | // ================================================================================================= |
| 9 | |
| 10 | package com.adobe.internal.xmp; |
| 11 | |
| 12 | import java.util.Calendar; |
| 13 | import java.util.Date; |
| 14 | import java.util.GregorianCalendar; |
| 15 | import java.util.TimeZone; |
| 16 | |
| 17 | import com.adobe.internal.xmp.impl.XMPDateTimeImpl; |
| 18 | |
| 19 | |
| 20 | /** |
| 21 | * A factory to create <code>XMPDateTime</code>-instances from a <code>Calendar</code> or an |
| 22 | * ISO 8601 string or for the current time. |
| 23 | * |
| 24 | * @author Stefan Makswit |
| 25 | * @version $Revision$ |
| 26 | * @since 16.02.2006 |
| 27 | */ |
| 28 | public final class XMPDateTimeFactory |
| 29 | { |
| 30 | /** The UTC TimeZone */ |
| 31 | private static final TimeZone UTC = TimeZone.getTimeZone("UTC"); |
| 32 | |
| 33 | |
| 34 | |
| 35 | /** Private constructor */ |
| 36 | private XMPDateTimeFactory() |
| 37 | { |
| 38 | // EMPTY |
| 39 | } |
| 40 | |
| 41 | |
| 42 | /** |
| 43 | * Creates an <code>XMPDateTime</code> from a <code>Calendar</code>-object. |
| 44 | * |
| 45 | * @param calendar a <code>Calendar</code>-object. |
| 46 | * @return An <code>XMPDateTime</code>-object. |
| 47 | */ |
| 48 | public static XMPDateTime createFromCalendar(Calendar calendar) |
| 49 | { |
| 50 | return new XMPDateTimeImpl(calendar); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | /** |
| 55 | * Creates an empty <code>XMPDateTime</code>-object. |
| 56 | * @return Returns an <code>XMPDateTime</code>-object. |
| 57 | */ |
| 58 | public static XMPDateTime create() |
| 59 | { |
| 60 | return new XMPDateTimeImpl(); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | /** |
| 65 | * Creates an <code>XMPDateTime</code>-object from initial values. |
| 66 | * @param year years |
| 67 | * @param month months from 1 to 12<br> |
| 68 | * <em>Note:</em> Remember that the month in {@link Calendar} is defined from 0 to 11. |
| 69 | * @param day days |
| 70 | * @return Returns an <code>XMPDateTime</code>-object. |
| 71 | */ |
| 72 | public static XMPDateTime create(int year, int month, int day) |
| 73 | { |
| 74 | XMPDateTime dt = new XMPDateTimeImpl(); |
| 75 | dt.setYear(year); |
| 76 | dt.setMonth(month); |
| 77 | dt.setDay(day); |
| 78 | return dt; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | /** |
| 83 | * Creates an <code>XMPDateTime</code>-object from initial values. |
| 84 | * @param year years |
| 85 | * @param month months from 1 to 12<br> |
| 86 | * <em>Note:</em> Remember that the month in {@link Calendar} is defined from 0 to 11. |
| 87 | * @param day days |
| 88 | * @param hour hours |
| 89 | * @param minute minutes |
| 90 | * @param second seconds |
| 91 | * @param nanoSecond nanoseconds |
| 92 | * @return Returns an <code>XMPDateTime</code>-object. |
| 93 | */ |
| 94 | public static XMPDateTime create(int year, int month, int day, |
| 95 | int hour, int minute, int second, int nanoSecond) |
| 96 | { |
| 97 | XMPDateTime dt = new XMPDateTimeImpl(); |
| 98 | dt.setYear(year); |
| 99 | dt.setMonth(month); |
| 100 | dt.setDay(day); |
| 101 | dt.setHour(hour); |
| 102 | dt.setMinute(minute); |
| 103 | dt.setSecond(second); |
| 104 | dt.setNanoSecond(nanoSecond); |
| 105 | return dt; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | /** |
| 110 | * Creates an <code>XMPDateTime</code> from an ISO 8601 string. |
| 111 | * |
| 112 | * @param strValue The ISO 8601 string representation of the date/time. |
| 113 | * @return An <code>XMPDateTime</code>-object. |
| 114 | * @throws XMPException When the ISO 8601 string is non-conform |
| 115 | */ |
| 116 | public static XMPDateTime createFromISO8601(String strValue) throws XMPException |
| 117 | { |
| 118 | return new XMPDateTimeImpl(strValue); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | /** |
| 123 | * Obtain the current date and time. |
| 124 | * |
| 125 | * @return Returns The returned time is UTC, properly adjusted for the local time zone. The |
| 126 | * resolution of the time is not guaranteed to be finer than seconds. |
| 127 | */ |
| 128 | public static XMPDateTime getCurrentDateTime() |
| 129 | { |
| 130 | return new XMPDateTimeImpl(new GregorianCalendar()); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /** |
| 135 | * Sets the local time zone without touching any other Any existing time zone value is replaced, |
| 136 | * the other date/time fields are not adjusted in any way. |
| 137 | * |
| 138 | * @param dateTime the <code>XMPDateTime</code> variable containing the value to be modified. |
| 139 | * @return Returns an updated <code>XMPDateTime</code>-object. |
| 140 | */ |
| 141 | public static XMPDateTime setLocalTimeZone(XMPDateTime dateTime) |
| 142 | { |
| 143 | Calendar cal = dateTime.getCalendar(); |
| 144 | cal.setTimeZone(TimeZone.getDefault()); |
| 145 | return new XMPDateTimeImpl(cal); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | /** |
| 150 | * Make sure a time is UTC. If the time zone is not UTC, the time is |
| 151 | * adjusted and the time zone set to be UTC. |
| 152 | * |
| 153 | * @param dateTime |
| 154 | * the <code>XMPDateTime</code> variable containing the time to |
| 155 | * be modified. |
| 156 | * @return Returns an updated <code>XMPDateTime</code>-object. |
| 157 | */ |
| 158 | public static XMPDateTime convertToUTCTime(XMPDateTime dateTime) |
| 159 | { |
| 160 | long timeInMillis = dateTime.getCalendar().getTimeInMillis(); |
| 161 | GregorianCalendar cal = new GregorianCalendar(UTC); |
| 162 | cal.setGregorianChange(new Date(Long.MIN_VALUE)); |
| 163 | cal.setTimeInMillis(timeInMillis); |
| 164 | return new XMPDateTimeImpl(cal); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | /** |
| 169 | * Make sure a time is local. If the time zone is not the local zone, the time is adjusted and |
| 170 | * the time zone set to be local. |
| 171 | * |
| 172 | * @param dateTime the <code>XMPDateTime</code> variable containing the time to be modified. |
| 173 | * @return Returns an updated <code>XMPDateTime</code>-object. |
| 174 | */ |
| 175 | public static XMPDateTime convertToLocalTime(XMPDateTime dateTime) |
| 176 | { |
| 177 | long timeInMillis = dateTime.getCalendar().getTimeInMillis(); |
| 178 | // has automatically local timezone |
| 179 | GregorianCalendar cal = new GregorianCalendar(); |
| 180 | cal.setTimeInMillis(timeInMillis); |
| 181 | return new XMPDateTimeImpl(cal); |
| 182 | } |
| 183 | } |