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