1 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 ; 27 import java.text.DateFormat ; 28 29 61 public class Time extends IQ { 62 63 private static SimpleDateFormat utcFormat = new SimpleDateFormat ("yyyyMMdd'T'HH:mm:ss"); 64 private static DateFormat displayFormat = DateFormat.getDateTimeInstance(); 65 66 private String utc = null; 67 private String tz = null; 68 private String display = null; 69 70 73 public Time() { 74 this(Calendar.getInstance()); 75 } 76 77 83 public Time(Calendar cal) { 84 TimeZone timeZone = cal.getTimeZone(); 85 tz = cal.getTimeZone().getID(); 86 display = displayFormat.format(cal.getTime()); 87 utc = utcFormat.format(new Date( 89 cal.getTimeInMillis() - timeZone.getOffset(cal.getTimeInMillis()))); 90 } 91 92 97 public Date getTime() { 98 if (utc == null) { 99 return null; 100 } 101 Date date = null; 102 try { 103 Calendar cal = Calendar.getInstance(); 104 cal.setTime(new Date(utcFormat.parse(utc).getTime() + 106 cal.getTimeZone().getOffset(cal.getTimeInMillis()))); 107 date = cal.getTime(); 108 } 109 catch (Exception e) { 110 e.printStackTrace(); 111 } 112 return date; 113 } 114 115 120 public void setTime(Date time) { 121 utc = utcFormat.format(new Date( 123 time.getTime() - TimeZone.getDefault().getOffset(time.getTime()))); 124 } 125 126 131 public String getUtc() { 132 return utc; 133 } 134 135 140 public void setUtc(String utc) { 141 this.utc = utc; 142 143 } 144 145 150 public String getTz() { 151 return tz; 152 } 153 154 159 public void setTz(String tz) { 160 this.tz = tz; 161 } 162 163 168 public String getDisplay() { 169 return display; 170 } 171 172 177 public void setDisplay(String display) { 178 this.display = display; 179 } 180 181 public String getChildElementXML() { 182 StringBuffer buf = new StringBuffer (); 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 |