KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smackx > packet > Time


1 /**
2  * $RCSfile$
3  * $Revision: 2520 $
4  * $Date: 2005-08-09 19:25:55 -0300 (Tue, 09 Aug 2005) $
5  *
6  * Copyright 2003-2004 Jive Software.
7  *
8  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */

20
21 package org.jivesoftware.smackx.packet;
22
23 import org.jivesoftware.smack.packet.IQ;
24
25 import java.util.*;
26 import java.text.SimpleDateFormat JavaDoc;
27 import java.text.DateFormat JavaDoc;
28
29 /**
30  * A Time IQ packet, which is used by XMPP clients to exchange their respective local
31  * times. Clients that wish to fully support the entitity time protocol should register
32  * a PacketListener for incoming time requests that then respond with the local time.
33  * This class can be used to request the time from other clients, such as in the
34  * following code snippet:
35  *
36  * <pre>
37  * // Request the time from a remote user.
38  * Time timeRequest = new Time();
39  * timeRequest.setType(IQ.Type.GET);
40  * timeRequest.setTo(someUser@example.com);
41  *
42  * // Create a packet collector to listen for a response.
43  * PacketCollector collector = con.createPacketCollector(
44  * new PacketIDFilter(timeRequest.getPacketID()));
45  *
46  * con.sendPacket(timeRequest);
47  *
48  * // Wait up to 5 seconds for a result.
49  * IQ result = (IQ)collector.nextResult(5000);
50  * if (result != null && result.getType() == IQ.Type.RESULT) {
51  * Time timeResult = (Time)result;
52  * // Do something with result...
53  * }</pre><p>
54  *
55  * Warning: this is an non-standard protocol documented by
56  * <a HREF="http://www.jabber.org/jeps/jep-0090.html">JEP-90</a>. Because this is a
57  * non-standard protocol, it is subject to change.
58  *
59  * @author Matt Tucker
60  */

61 public class Time extends IQ {
62
63     private static SimpleDateFormat JavaDoc utcFormat = new SimpleDateFormat JavaDoc("yyyyMMdd'T'HH:mm:ss");
64     private static DateFormat JavaDoc displayFormat = DateFormat.getDateTimeInstance();
65
66     private String JavaDoc utc = null;
67     private String JavaDoc tz = null;
68     private String JavaDoc display = null;
69
70     /**
71      * Creates a new Time instance with empty values for all fields.
72      */

73     public Time() {
74         this(Calendar.getInstance());
75     }
76
77     /**
78      * Creates a new Time instance using the specified calendar instance as
79      * the time value to send.
80      *
81      * @param cal the time value.
82      */

83     public Time(Calendar cal) {
84         TimeZone timeZone = cal.getTimeZone();
85         tz = cal.getTimeZone().getID();
86         display = displayFormat.format(cal.getTime());
87         // Convert local time to the UTC time.
88
utc = utcFormat.format(new Date(
89                 cal.getTimeInMillis() - timeZone.getOffset(cal.getTimeInMillis())));
90     }
91
92     /**
93      * Returns the local time or <tt>null</tt> if the time hasn't been set.
94      *
95      * @return the lcocal time.
96      */

97     public Date getTime() {
98         if (utc == null) {
99             return null;
100         }
101         Date date = null;
102         try {
103             Calendar cal = Calendar.getInstance();
104             // Convert the UTC time to local time.
105
cal.setTime(new Date(utcFormat.parse(utc).getTime() +
106                     cal.getTimeZone().getOffset(cal.getTimeInMillis())));
107             date = cal.getTime();
108         }
109         catch (Exception JavaDoc e) {
110             e.printStackTrace();
111         }
112         return date;
113     }
114
115     /**
116      * Sets the time using the local time.
117      *
118      * @param time the current local time.
119      */

120     public void setTime(Date time) {
121         // Convert local time to UTC time.
122
utc = utcFormat.format(new Date(
123                 time.getTime() - TimeZone.getDefault().getOffset(time.getTime())));
124     }
125
126     /**
127      * Returns the time as a UTC formatted String using the format CCYYMMDDThh:mm:ss.
128      *
129      * @return the time as a UTC formatted String.
130      */

131     public String JavaDoc getUtc() {
132         return utc;
133     }
134
135     /**
136      * Sets the time using UTC formatted String in the format CCYYMMDDThh:mm:ss.
137      *
138      * @param utc the time using a formatted String.
139      */

140     public void setUtc(String JavaDoc utc) {
141         this.utc = utc;
142
143     }
144
145     /**
146      * Returns the time zone.
147      *
148      * @return the time zone.
149      */

150     public String JavaDoc getTz() {
151         return tz;
152     }
153
154     /**
155      * Sets the time zone.
156      *
157      * @param tz the time zone.
158      */

159     public void setTz(String JavaDoc tz) {
160         this.tz = tz;
161     }
162
163     /**
164      * Returns the local (non-utc) time in human-friendly format.
165      *
166      * @return the local time in human-friendly format.
167      */

168     public String JavaDoc getDisplay() {
169         return display;
170     }
171
172     /**
173      * Sets the local time in human-friendly format.
174      *
175      * @param display the local time in human-friendly format.
176      */

177     public void setDisplay(String JavaDoc display) {
178         this.display = display;
179     }
180
181     public String JavaDoc getChildElementXML() {
182         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
183         buf.append("<query xmlns=\"jabber:iq:time\">");
184         if (utc != null) {
185             buf.append("<utc>").append(utc).append("</utc>");
186         }
187         if (tz != null) {
188             buf.append("<tz>").append(tz).append("</tz>");
189         }
190         if (display != null) {
191             buf.append("<display>").append(display).append("</display>");
192         }
193         buf.append("</query>");
194         return buf.toString();
195     }
196 }
Popular Tags