001
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
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
044 @Deprecated
045 public PrettyDateFormat(long companyId, Locale locale, TimeZone timeZone) {
046 this(locale, timeZone);
047 }
048
049 @Override
050 public StringBuffer format(Date date, StringBuffer sb, FieldPosition pos) {
051 String dateString = StringPool.NBSP;
052
053 if (date == null) {
054 return sb.append(dateString);
055 }
056
057 Date today = new Date();
058
059 Calendar cal = Calendar.getInstance(_timeZone, _locale);
060
061 cal.setTime(today);
062 cal.add(Calendar.DATE, -1);
063
064 Date yesterday = cal.getTime();
065
066 Format dateFormatDate = FastDateFormatFactoryUtil.getDate(
067 _locale, _timeZone);
068 Format dateFormatDateTime = FastDateFormatFactoryUtil.getDateTime(
069 _locale, _timeZone);
070 Format dateFormatTime = FastDateFormatFactoryUtil.getTime(
071 _locale, _timeZone);
072
073 dateString = dateFormatDate.format(date);
074
075 if (dateString.equals(dateFormatDate.format(today))) {
076 dateString =
077 _todayString + StringPool.SPACE +
078 dateFormatTime.format(date);
079 }
080 else if (dateString.equals(dateFormatDate.format(yesterday))) {
081 dateString =
082 _yesterdayString + StringPool.SPACE +
083 dateFormatTime.format(date);
084 }
085 else {
086 dateString = dateFormatDateTime.format(date);
087 }
088
089 return sb.append(dateString);
090 }
091
092 @Override
093 public Date parse(String source, ParsePosition pos) {
094 Format dateFormatDate = FastDateFormatFactoryUtil.getDate(
095 _locale, _timeZone);
096 DateFormat dateFormatDateTime = DateFormatFactoryUtil.getDateTime(
097 _locale, _timeZone);
098
099 Date today = new Date();
100
101 String dateString = source.substring(pos.getIndex());
102
103 if (dateString.startsWith(_todayString)) {
104 dateString = dateString.replaceFirst(
105 _todayString, dateFormatDate.format(today));
106 }
107 else if (dateString.startsWith(_yesterdayString)) {
108 Calendar cal = Calendar.getInstance(_timeZone, _locale);
109
110 cal.setTime(today);
111 cal.add(Calendar.DATE, -1);
112
113 Date yesterday = cal.getTime();
114
115 dateString = dateString.replaceFirst(
116 _todayString, dateFormatDate.format(yesterday));
117 }
118
119 return dateFormatDateTime.parse(dateString, new ParsePosition(0));
120 }
121
122 private final Locale _locale;
123 private final TimeZone _timeZone;
124 private final String _todayString;
125 private final String _yesterdayString;
126
127 }