001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    
019    import java.text.DateFormat;
020    import java.text.FieldPosition;
021    import java.text.Format;
022    import java.text.ParsePosition;
023    
024    import java.util.Calendar;
025    import java.util.Date;
026    import java.util.Locale;
027    import java.util.TimeZone;
028    
029    /**
030     * @author Alexander Chow
031     */
032    public class PrettyDateFormat extends DateFormat {
033    
034            public PrettyDateFormat(Locale locale, TimeZone timeZone) {
035                    _locale = locale;
036                    _timeZone = timeZone;
037                    _todayString = LanguageUtil.get(_locale, "today");
038                    _yesterdayString = LanguageUtil.get(_locale, "yesterday");
039            }
040    
041            /**
042             * @deprecated
043             */
044            public PrettyDateFormat(long companyId, Locale locale, TimeZone timeZone) {
045                    this(locale, timeZone);
046            }
047    
048            public StringBuffer format(Date date, StringBuffer sb, FieldPosition pos) {
049                    String dateString = StringPool.NBSP;
050    
051                    if (date != null) {
052                            Date today = new Date();
053    
054                            Calendar cal = Calendar.getInstance(_timeZone, _locale);
055    
056                            cal.setTime(today);
057                            cal.add(Calendar.DATE, -1);
058    
059                            Date yesterday = cal.getTime();
060    
061                            Format dateFormatDate = FastDateFormatFactoryUtil.getDate(
062                                    _locale, _timeZone);
063                            Format dateFormatDateTime = FastDateFormatFactoryUtil.getDateTime(
064                                    _locale, _timeZone);
065                            Format dateFormatTime = FastDateFormatFactoryUtil.getTime(
066                                    _locale, _timeZone);
067    
068                            dateString = dateFormatDate.format(date);
069    
070                            if (dateString.equals(dateFormatDate.format(today))) {
071                                    dateString =
072                                            _todayString + StringPool.SPACE +
073                                                    dateFormatTime.format(date);
074                            }
075                            else if (dateString.equals(dateFormatDate.format(yesterday))) {
076                                    dateString =
077                                            _yesterdayString + StringPool.SPACE +
078                                                    dateFormatTime.format(date);
079                            }
080                            else {
081                                    dateString = dateFormatDateTime.format(date);
082                            }
083                    }
084    
085                    return sb.append(dateString);
086            }
087    
088            public Date parse(String source, ParsePosition pos) {
089                    Format dateFormatDate = FastDateFormatFactoryUtil.getDate(
090                            _locale, _timeZone);
091                    DateFormat dateFormatDateTime = DateFormatFactoryUtil.getDateTime(
092                            _locale, _timeZone);
093    
094                    Date today = new Date();
095    
096                    String dateString = source.substring(pos.getIndex());
097    
098                    if (dateString.startsWith(_todayString)) {
099                            dateString = dateString.replaceFirst(
100                                    _todayString, dateFormatDate.format(today));
101                    }
102                    else if (dateString.startsWith(_yesterdayString)) {
103                            Calendar cal = Calendar.getInstance(_timeZone, _locale);
104    
105                            cal.setTime(today);
106                            cal.add(Calendar.DATE, -1);
107    
108                            Date yesterday = cal.getTime();
109    
110                            dateString = dateString.replaceFirst(
111                                    _todayString, dateFormatDate.format(yesterday));
112                    }
113    
114                    return dateFormatDateTime.parse(dateString, new ParsePosition(0));
115            }
116    
117            private Locale _locale;
118            private TimeZone _timeZone;
119            private String _todayString;
120            private String _yesterdayString;
121    
122    }