1 31 32 package org.opencms.util; 33 34 import org.opencms.i18n.CmsLocaleManager; 35 36 import java.text.DateFormat ; 37 import java.text.ParseException ; 38 import java.text.SimpleDateFormat ; 39 import java.util.Calendar ; 40 import java.util.Date ; 41 import java.util.GregorianCalendar ; 42 import java.util.Locale ; 43 import java.util.TimeZone ; 44 45 55 public final class CmsDateUtil { 56 57 58 protected static final TimeZone GMT_TIMEZONE = TimeZone.getTimeZone("GMT"); 59 60 61 protected static final DateFormat HEADER_DEFAULT = new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US); 62 63 64 protected static final DateFormat OLD_COOKIE = new SimpleDateFormat ("EEE, dd-MMM-yyyy HH:mm:ss z", Locale.US); 65 66 69 private CmsDateUtil() { 70 71 } 73 74 83 public static String getDate(Date date, int format, Locale locale) { 84 85 DateFormat df = DateFormat.getDateInstance(format, locale); 86 return df.format(date); 87 } 88 89 97 public static String getDateShort(long time) { 98 99 return getDate(new Date (time), DateFormat.SHORT, CmsLocaleManager.getDefaultLocale()); 100 } 101 102 111 public static String getDateTime(Date date, int format, Locale locale) { 112 113 DateFormat df = DateFormat.getDateInstance(format, locale); 114 DateFormat tf = DateFormat.getTimeInstance(format, locale); 115 StringBuffer buf = new StringBuffer (); 116 buf.append(df.format(date)); 117 buf.append(" "); 118 buf.append(tf.format(date)); 119 return buf.toString(); 120 } 121 122 130 public static String getDateTimeShort(long time) { 131 132 return getDateTime(new Date (time), DateFormat.SHORT, CmsLocaleManager.getDefaultLocale()); 133 } 134 135 142 public static int getDaysPassedSince(Date dateLastModified) { 143 144 GregorianCalendar now = new GregorianCalendar (); 145 GregorianCalendar lastModified = (GregorianCalendar )now.clone(); 146 lastModified.setTimeInMillis(dateLastModified.getTime()); 147 return now.get(Calendar.DAY_OF_YEAR) 148 - lastModified.get(Calendar.DAY_OF_YEAR) 149 + (now.get(Calendar.YEAR) - lastModified.get(Calendar.YEAR)) 150 * 365; 151 } 152 153 160 public static String getHeaderDate(long time) { 161 162 if (HEADER_DEFAULT.getTimeZone() != GMT_TIMEZONE) { 163 HEADER_DEFAULT.setTimeZone(GMT_TIMEZONE); 165 } 166 167 return HEADER_DEFAULT.format(new Date (time)); 168 } 169 170 177 public static String getOldCookieDate(long time) { 178 179 if (OLD_COOKIE.getTimeZone() != GMT_TIMEZONE) { 180 OLD_COOKIE.setTimeZone(GMT_TIMEZONE); 182 } 183 184 return OLD_COOKIE.format(new Date (time)); 185 } 186 187 195 public static long parseHeaderDate(String timestamp) throws ParseException { 196 197 return HEADER_DEFAULT.parse(timestamp).getTime(); 198 } 199 } | Popular Tags |