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.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.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 * @since 16.02.2006 25 */ 26 public final class XMPDateTimeFactory 27 { 28 /** The UTC TimeZone */ 29 private static final TimeZone UTC = TimeZone.getTimeZone("UTC"); 30 31 32 33 /** Private constructor */ 34 private XMPDateTimeFactory() 35 { 36 // EMPTY 37 } 38 39 40 /** 41 * Creates an <code>XMPDateTime</code> from a <code>Calendar</code>-object. 42 * 43 * @param calendar a <code>Calendar</code>-object. 44 * @return An <code>XMPDateTime</code>-object. 45 */ 46 public static XMPDateTime createFromCalendar(Calendar calendar) 47 { 48 return new XMPDateTimeImpl(calendar); 49 } 50 51 52 /** 53 * Creates an <code>XMPDateTime</code>-object from initial values. 54 * @param year years 55 * @param month months from 1 to 12<br> 56 * <em>Note:</em> Remember that the month in {@link Calendar} is defined from 0 to 11. 57 * @param day days 58 * @param hour hours 59 * @param minute minutes 60 * @param second seconds 61 * @param nanoSecond nanoseconds 62 * @return Returns an <code>XMPDateTime</code>-object. 63 */ 64 public static XMPDateTime create(int year, int month, int day, 65 int hour, int minute, int second, int nanoSecond) 66 { 67 XMPDateTime dt = new XMPDateTimeImpl(); 68 dt.setYear(year); 69 dt.setMonth(month); 70 dt.setDay(day); 71 dt.setHour(hour); 72 dt.setMinute(minute); 73 dt.setSecond(second); 74 dt.setNanoSecond(nanoSecond); 75 return dt; 76 } 77 78 79 /** 80 * Creates an <code>XMPDateTime</code> from an ISO 8601 string. 81 * 82 * @param strValue The ISO 8601 string representation of the date/time. 83 * @return An <code>XMPDateTime</code>-object. 84 * @throws XMPException When the ISO 8601 string is non-conform 85 */ 86 public static XMPDateTime createFromISO8601(String strValue) throws XMPException 87 { 88 return new XMPDateTimeImpl(strValue); 89 } 90 91 92 /** 93 * Obtain the current date and time. 94 * 95 * @return Returns The returned time is UTC, properly adjusted for the local time zone. The 96 * resolution of the time is not guaranteed to be finer than seconds. 97 */ 98 public static XMPDateTime getCurrentDateTime() 99 { 100 return new XMPDateTimeImpl(new GregorianCalendar()); 101 } 102 103 104 /** 105 * Sets the local time zone without touching any other Any existing time zone value is replaced, 106 * the other date/time fields are not adjusted in any way. 107 * 108 * @param dateTime the <code>XMPDateTime</code> variable containing the value to be modified. 109 * @return Returns an updated <code>XMPDateTime</code>-object. 110 */ 111 public static XMPDateTime setLocalTimeZone(XMPDateTime dateTime) 112 { 113 Calendar cal = dateTime.getCalendar(); 114 cal.setTimeZone(TimeZone.getDefault()); 115 return new XMPDateTimeImpl(cal); 116 } 117 118 119 /** 120 * Make sure a time is UTC. If the time zone is not UTC, the time is 121 * adjusted and the time zone set to be UTC. 122 * 123 * @param dateTime 124 * the <code>XMPDateTime</code> variable containing the time to 125 * be modified. 126 * @return Returns an updated <code>XMPDateTime</code>-object. 127 */ 128 public static XMPDateTime convertToUTCTime(XMPDateTime dateTime) 129 { 130 long timeInMillis = dateTime.getCalendar().getTimeInMillis(); 131 GregorianCalendar cal = new GregorianCalendar(UTC); 132 cal.setGregorianChange(new Date(Long.MIN_VALUE)); 133 cal.setTimeInMillis(timeInMillis); 134 return new XMPDateTimeImpl(cal); 135 } 136 137 138 /** 139 * Make sure a time is local. If the time zone is not the local zone, the time is adjusted and 140 * the time zone set to be local. 141 * 142 * @param dateTime the <code>XMPDateTime</code> variable containing the time to be modified. 143 * @return Returns an updated <code>XMPDateTime</code>-object. 144 */ 145 public static XMPDateTime convertToLocalTime(XMPDateTime dateTime) 146 { 147 long timeInMillis = dateTime.getCalendar().getTimeInMillis(); 148 // has automatically local timezone 149 GregorianCalendar cal = new GregorianCalendar(); 150 cal.setTimeInMillis(timeInMillis); 151 return new XMPDateTimeImpl(cal); 152 } 153 }