1 34 package net.myvietnam.mvncore.util; 35 36 import java.sql.Timestamp ; 37 import java.text.DateFormat ; 38 import java.text.SimpleDateFormat ; 39 import java.util.*; 40 import java.util.Date ; 41 42 import net.myvietnam.mvncore.MVNCoreConfig; 43 44 47 public final class DateUtil { 48 49 public static final long SECOND = 1000; 50 public static final long MINUTE = SECOND * 60; 51 public static final long HOUR = MINUTE * 60; 52 public static final long DAY = HOUR * 24; 53 public static final long WEEK = DAY * 7; 54 public static final long YEAR = DAY * 365; 56 59 public static final long GMT_VIETNAM_TIME_OFFSET = HOUR * 7; 60 61 65 public static final String RFC_822_DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss Z"; 66 67 72 public static final String ISO_8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ"; 73 74 77 public static final String UTC_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'"; 78 79 82 84 private static DateFormat ddMMyyyyFormat = new SimpleDateFormat ("dd/MM/yyyy"); 85 private static DateFormat yyyyMMddFormat = new SimpleDateFormat ("yyyy-MM-dd"); 86 private static DateFormat rfc822Format = new SimpleDateFormat (RFC_822_DATE_FORMAT, Locale.US); 87 private static DateFormat iso8601Format = new SimpleDateFormat (ISO_8601_DATE_FORMAT, Locale.US); 88 private static DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT); 89 private static DateFormat datetimeFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT); 90 private static DateFormat viDateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, new Locale("vi", "VN")); 91 private static DateFormat viDatetimeFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, new Locale("vi", "VN")); 92 private static DateFormat headerTimeFormat = new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US); 93 95 static { 96 TimeZone gmt = TimeZone.getTimeZone("GMT"); 97 headerTimeFormat.setTimeZone(gmt); 98 } 99 100 103 private DateUtil() { } 105 106 public static synchronized String getDateDDMMYYYY(Date date) { 107 return ddMMyyyyFormat.format(date); 108 } 109 110 public static synchronized String getDateYYYYMMDD(Date date) { 111 return yyyyMMddFormat.format(date); 112 } 113 114 public static synchronized String getDateRFC822(Date date) { 115 return rfc822Format.format(date); 116 } 117 118 public static synchronized String getDateISO8601(Date date) { 119 String formattedDate = iso8601Format.format(date); 120 int length = formattedDate.length(); 121 if (length > 2) { 122 return new StringBuffer (64).append(formattedDate.substring(0, length - 2)).append(":").append(formattedDate.substring(length - 2)).toString(); 123 } 124 return formattedDate; 125 } 126 127 public static synchronized String getHTTPHeaderTime(Date date) { 128 return headerTimeFormat.format(date); 129 } 130 131 public static synchronized String formatDate(Date date) { 132 return dateFormat.format(date); 133 } 134 135 public static synchronized String formatDateTime(Date date) { 136 return datetimeFormat.format(date); 137 } 138 139 public static synchronized String formatViDate(Date date) { 140 return viDateFormat.format(date); 141 } 142 143 public static synchronized String formatViDateTime(Date date) { 144 return viDatetimeFormat.format(date); 145 } 146 147 public static String getViDateTimeDesc() { 148 return formatViDateTime(getVietnamDateFromGMTDate(getCurrentGMTTimestamp())); 149 } 150 151 public static String getViDateDesc() { 152 return formatViDate(getVietnamDateFromGMTDate(getCurrentGMTTimestamp())); 153 } 154 155 public static Timestamp getCurrentGMTTimestamp() { 156 return new Timestamp (System.currentTimeMillis() - (HOUR * MVNCoreConfig.getServerHourOffset())); 157 } 158 159 public static void updateCurrentGMTTimestamp(Timestamp timeToUpdate) { 160 timeToUpdate.setTime(System.currentTimeMillis() - (HOUR * MVNCoreConfig.getServerHourOffset())); 161 } 162 163 public static Date getVietnamDateFromGMTDate(Date date) { 164 return new Date (date.getTime() + GMT_VIETNAM_TIME_OFFSET); 165 } 166 167 171 172 public static Date convertGMTDate(Date gmtDate, double hourOffset) { 173 return new Date (gmtDate.getTime() + (long)(hourOffset*HOUR)); 174 } 175 176 public static Timestamp convertGMTTimestamp(Timestamp gmtTimestamp, double hourOffset) { 177 return new Timestamp (gmtTimestamp.getTime() + (long)(hourOffset*HOUR)); 178 } 179 180 public static Timestamp getCurrentGMTTimestampExpiredYear(int offsetYear) { 181 Calendar now = Calendar.getInstance(); 183 now.add(Calendar.YEAR, offsetYear); 184 return new Timestamp (now.getTime().getTime()); 185 } 186 187 public static Timestamp getCurrentGMTTimestampExpiredMonth(int offsetMonth) { 188 Calendar now = Calendar.getInstance(); 189 now.add(Calendar.MONTH, offsetMonth); 190 return new Timestamp (now.getTime().getTime()); 191 } 192 193 public static Timestamp getCurrentGMTTimestampExpiredDay(int offsetDay) { 194 Calendar now = Calendar.getInstance(); 195 now.add(Calendar.DATE,offsetDay); 196 return new Timestamp (now.getTime().getTime()); 197 } 198 199 public static String format(Date date, String pattern) { 200 DateFormat formatter = new SimpleDateFormat (pattern, Locale.US); 201 return formatter.format(date); 202 } 203 204 public static String formatDuration(long duration, String pattern) { 205 DurationFormater time = new DurationFormater(duration, pattern); 206 return time.toString(); 207 } 208 209 public static String formatDuration(long duration) { 210 DurationFormater time = new DurationFormater(duration, null); 211 return time.toString(); 212 } 213 214 public static void main (String [] agrs) { 215 long duration = (long)1000 * 60 * 60 *24 * 130 + (long)1000 * 60 * 80; 216 System.out.println(duration); 217 System.out.println("Duration of " + duration + " duration = " + formatDuration(duration)); 218 Date d = new Date (); 219 System.out.println(" Date is " + DateUtil.getDateISO8601(d)); 220 } 221 } 222 223 class DurationFormater { 224 public static final long MILISECONDS_PER_SECOND = 1000; 225 public static final long SECONDS_PER_MINUTE = 60; 226 public static final long MINUTES_PER_HOUR = 60; 227 public static final long HOURS_PER_DAY = 24; 228 229 public static final int MILISECOND = 0; 230 public static final int SECOND = 1; 231 public static final int MINUTE = 2; 232 public static final int HOUR = 3; 233 public static final int DAY = 4; 234 235 236 public static final String PATTERNS[] = { 237 "@ms", "@s", "@m", "@h", "@d" 238 }; 239 private static final long[] AMOUNTS = { 240 MILISECONDS_PER_SECOND, 241 SECONDS_PER_MINUTE, 242 MINUTES_PER_HOUR, 243 HOURS_PER_DAY 244 }; 245 private static long[] times = new long[5]; 246 private long time; 247 private String pattern; 248 private boolean detail = false; 249 250 public DurationFormater() { 251 } 252 253 public DurationFormater(long time, String pattern) { 254 this.time = time; 255 this.pattern = pattern; 256 update(); 257 } 258 259 public DurationFormater(long time) { 260 this.time = time; 261 update(); 262 } 263 264 private void update() { 265 long remain = time; 266 for (int i = 0; i < AMOUNTS.length; i++) { 267 times[i] = remain % AMOUNTS[i]; 268 remain = remain / AMOUNTS[i]; 269 } 270 times[DAY] = (int) remain; 271 } 272 273 279 public void setPattern(String pattern) { 280 this.pattern = pattern; 281 } 282 283 public long getTime() { 284 return time; 285 } 286 287 public void setTime(long duration) { 288 time = duration; 289 update(); 290 } 291 292 public long getMiliseconds() { 293 return times[MILISECOND]; 294 } 295 296 public long getSeconds() { 297 return times[SECOND]; 298 } 299 300 public long getMinutes() { 301 return times[MINUTE]; 302 } 303 304 public long getHours() { 305 return times[HOUR]; 306 } 307 308 public long getDays() { 309 return times[DAY]; 310 } 311 312 public void setDetail(boolean detail) { 313 this.detail = detail; 314 } 315 316 public String getString() { 317 StringBuffer buffer = new StringBuffer (1024); 318 buffer.append(pattern); 319 for (int i = 0; i < PATTERNS.length; i++) { 320 int start = -1; 321 int end = -1; 322 while ((start = buffer.toString().indexOf(PATTERNS[i])) > -1) { 324 end = start + PATTERNS[i].length(); 325 buffer.replace(start, end, String.valueOf(times[i])); 326 } 327 } 328 return buffer.toString(); 329 } 330 331 public String toString() { 332 if (pattern != null) { 333 return getString(); 334 } 335 336 StringBuffer desc = new StringBuffer (256); 337 if (times[DAY] > 0) { 338 desc.append(checkPlural(times[DAY], "day")); 339 } 340 if (times[HOUR] > 0) { 341 desc.append(checkPlural(times[HOUR], "hour")); 342 } 343 if ((times[MINUTE] > 0) || (times[DAY] == 0 && times[MINUTE] == 0)) { 344 desc.append(checkPlural(times[MINUTE], "minute")); 345 } 346 if (detail) { 347 desc.append(checkPlural(times[SECOND], "second")); 348 desc.append(checkPlural(times[MILISECOND], "milisecond")); 349 } 350 return desc.toString(); 351 } 352 353 private static String checkPlural(long amount, String unit) { 354 StringBuffer desc = new StringBuffer (20); 355 desc.append(amount).append(" ").append(unit); 356 if (amount > 1) { 357 desc.append("s"); 358 } 359 return desc.append(" ").toString(); 360 } 361 } 362 | Popular Tags |