001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.util;
022:
023: import com.liferay.portal.kernel.language.LanguageUtil;
024: import com.liferay.portal.kernel.util.StringPool;
025:
026: import java.text.DateFormat;
027: import java.text.FieldPosition;
028: import java.text.ParsePosition;
029:
030: import java.util.Calendar;
031: import java.util.Date;
032: import java.util.Locale;
033: import java.util.TimeZone;
034:
035: /**
036: * <a href="PrettyDateFormat.java.html"><b><i>View Source</i></b></a>
037: *
038: * @author Alexander Chow
039: *
040: */
041: public class PrettyDateFormat extends DateFormat {
042:
043: public PrettyDateFormat(long companyId, Locale locale,
044: TimeZone timeZone) {
045: _companyId = companyId;
046: _locale = locale;
047: _timeZone = timeZone;
048: _todayString = LanguageUtil.get(_companyId, _locale, "today");
049: _yesterdayString = LanguageUtil.get(_companyId, _locale,
050: "yesterday");
051: }
052:
053: public StringBuffer format(Date date, StringBuffer sb,
054: FieldPosition pos) {
055: String dateString = StringPool.NBSP;
056:
057: if (date != null) {
058: Date today = new Date();
059:
060: Calendar cal = Calendar.getInstance(_timeZone, _locale);
061:
062: cal.setTime(today);
063: cal.add(Calendar.DATE, -1);
064:
065: Date yesterday = cal.getTime();
066:
067: DateFormat dateFormatDate = DateFormats.getDate(_locale,
068: _timeZone);
069: DateFormat dateFormatDateTime = DateFormats.getDateTime(
070: _locale, _timeZone);
071: DateFormat dateFormatTime = DateFormats.getTime(_locale,
072: _timeZone);
073:
074: dateString = dateFormatDate.format(date);
075:
076: if (dateString.equals(dateFormatDate.format(today))) {
077: dateString = _todayString + StringPool.SPACE
078: + dateFormatTime.format(date);
079: } else if (dateString.equals(dateFormatDate
080: .format(yesterday))) {
081: dateString = _yesterdayString + StringPool.SPACE
082: + dateFormatTime.format(date);
083: } else {
084: dateString = dateFormatDateTime.format(date);
085: }
086: }
087:
088: return sb.append(dateString);
089: }
090:
091: public Date parse(String source, ParsePosition pos) {
092: DateFormat dateFormatDate = DateFormats.getDate(_locale,
093: _timeZone);
094:
095: DateFormat dateFormatDateTime = DateFormats.getDateTime(
096: _locale, _timeZone);
097:
098: Date today = new Date();
099:
100: String dateString = source.substring(pos.getIndex());
101:
102: if (dateString.startsWith(_todayString)) {
103: dateString.replaceFirst(_todayString, dateFormatDate
104: .format(today));
105: } else if (dateString.startsWith(_yesterdayString)) {
106: Calendar cal = Calendar.getInstance(_timeZone, _locale);
107:
108: cal.setTime(today);
109: cal.add(Calendar.DATE, -1);
110:
111: Date yesterday = cal.getTime();
112:
113: dateString.replaceFirst(_todayString, dateFormatDate
114: .format(yesterday));
115: }
116:
117: return dateFormatDateTime.parse(dateString,
118: new ParsePosition(0));
119: }
120:
121: private long _companyId;
122: private Locale _locale;
123: private TimeZone _timeZone;
124: private String _todayString;
125: private String _yesterdayString;
126:
127: }
|