001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software GmbH & Co. KG, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.util; 029 030import org.opencms.i18n.CmsLocaleManager; 031 032import java.text.DateFormat; 033import java.text.ParseException; 034import java.text.SimpleDateFormat; 035import java.util.Calendar; 036import java.util.Date; 037import java.util.GregorianCalendar; 038import java.util.Locale; 039import java.util.TimeZone; 040 041/** 042 * 043 * Utilities to get and set formated dates in OpenCms.<p> 044 * 045 * @since 6.0.0 046 */ 047public final class CmsDateUtil { 048 049 /** The "GMT" time zone, used when formatting http headers. */ 050 protected static final TimeZone GMT_TIMEZONE = TimeZone.getTimeZone("GMT"); 051 052 /** The default format to use when formatting http headers. */ 053 protected static final DateFormat HEADER_DEFAULT = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US); 054 055 /** The default format to use when formatting old cookies. */ 056 protected static final DateFormat OLD_COOKIE = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z", Locale.US); 057 058 /** 059 * Hides the public constructor.<p> 060 */ 061 private CmsDateUtil() { 062 063 // noop 064 } 065 066 /** 067 * Returns a formated date String from a Date value, 068 * the formatting based on the provided options.<p> 069 * 070 * @param date the Date object to format as String 071 * @param format the format to use, see {@link DateFormat} for possible values 072 * @param locale the locale to use 073 * @return the formatted date 074 */ 075 public static String getDate(Date date, int format, Locale locale) { 076 077 DateFormat df = DateFormat.getDateInstance(format, locale); 078 return df.format(date); 079 } 080 081 /** 082 * Returns a formated date String form a timestamp value, 083 * the formatting based on the OpenCms system default locale 084 * and the {@link DateFormat#SHORT} date format.<p> 085 * 086 * @param time the time value to format as date 087 * @return the formatted date 088 */ 089 public static String getDateShort(long time) { 090 091 return getDate(new Date(time), DateFormat.SHORT, CmsLocaleManager.getDefaultLocale()); 092 } 093 094 /** 095 * Returns a formated date and time String from a Date value, 096 * the formatting based on the provided options.<p> 097 * 098 * @param date the Date object to format as String 099 * @param format the format to use, see {@link DateFormat} for possible values 100 * @param locale the locale to use 101 * @return the formatted date 102 */ 103 public static String getDateTime(Date date, int format, Locale locale) { 104 105 DateFormat df = DateFormat.getDateInstance(format, locale); 106 DateFormat tf = DateFormat.getTimeInstance(format, locale); 107 StringBuffer buf = new StringBuffer(); 108 buf.append(df.format(date)); 109 buf.append(" "); 110 buf.append(tf.format(date)); 111 return buf.toString(); 112 } 113 114 /** 115 * Returns a formated date and time String form a timestamp value, 116 * the formatting based on the OpenCms system default locale 117 * and the {@link DateFormat#SHORT} date format.<p> 118 * 119 * @param time the time value to format as date 120 * @return the formatted date 121 */ 122 public static String getDateTimeShort(long time) { 123 124 return getDateTime(new Date(time), DateFormat.SHORT, CmsLocaleManager.getDefaultLocale()); 125 } 126 127 /** 128 * Returns the number of days passed since a specific date.<p> 129 * 130 * @param dateLastModified the date to compute the passed days from 131 * 132 * @return the number of days passed since a specific date 133 */ 134 public static int getDaysPassedSince(Date dateLastModified) { 135 136 GregorianCalendar now = new GregorianCalendar(); 137 GregorianCalendar lastModified = (GregorianCalendar)now.clone(); 138 lastModified.setTimeInMillis(dateLastModified.getTime()); 139 return (now.get(Calendar.DAY_OF_YEAR) - lastModified.get(Calendar.DAY_OF_YEAR)) 140 + ((now.get(Calendar.YEAR) - lastModified.get(Calendar.YEAR)) * 365); 141 } 142 143 /** 144 * Returns a formated date and time String form a timestamp value based on the 145 * HTTP-Header date format.<p> 146 * 147 * @param time the time value to format as date 148 * @return the formatted date 149 */ 150 synchronized public static String getHeaderDate(long time) { 151 152 if (HEADER_DEFAULT.getTimeZone() != GMT_TIMEZONE) { 153 // ensure GMT is used as time zone for the header generation 154 HEADER_DEFAULT.setTimeZone(GMT_TIMEZONE); 155 } 156 157 return HEADER_DEFAULT.format(new Date(time)); 158 } 159 160 /** 161 * Returns a formatted date and time String form a timestamp value based on the 162 * (old) Netscape cookie date format.<p> 163 * 164 * @param time the time value to format as date 165 * @return the formatted date 166 */ 167 synchronized public static String getOldCookieDate(long time) { 168 169 if (OLD_COOKIE.getTimeZone() != GMT_TIMEZONE) { 170 // ensure GMT is used as time zone for the header generation 171 OLD_COOKIE.setTimeZone(GMT_TIMEZONE); 172 } 173 174 try { 175 return OLD_COOKIE.format(new Date(time)); 176 } catch (Throwable t) { 177 return OLD_COOKIE.format(new Date()); 178 } 179 } 180 181 /** 182 * Returns the long value of a date created by the given integer values.<p> 183 * 184 * @param year the integer value of year 185 * @param month the integer value of month 186 * @param date the integer value of date 187 * 188 * @return the long value of a date created by the given integer values 189 */ 190 public static long parseDate(int year, int month, int date) { 191 192 Calendar calendar = Calendar.getInstance(); 193 calendar.set(year, month, date); 194 return calendar.getTime().getTime(); 195 } 196 197 /** 198 * Parses a formated date and time string in HTTP-Header date format and returns the 199 * time value.<p> 200 * 201 * @param timestamp the timestamp in HTTP-Header date format 202 * @return time value as long 203 * @throws ParseException if parsing fails 204 */ 205 synchronized public static long parseHeaderDate(String timestamp) throws ParseException { 206 207 return HEADER_DEFAULT.parse(timestamp).getTime(); 208 } 209}