1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  import com.liferay.portal.kernel.language.LanguageUtil;
26  
27  import java.text.DateFormat;
28  import java.text.FieldPosition;
29  import java.text.ParsePosition;
30  
31  import java.util.Calendar;
32  import java.util.Date;
33  import java.util.Locale;
34  import java.util.TimeZone;
35  
36  /**
37   * <a href="PrettyDateFormat.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Alexander Chow
40   *
41   */
42  public class PrettyDateFormat extends DateFormat {
43  
44      public PrettyDateFormat(long companyId, Locale locale, TimeZone timeZone) {
45          _companyId = companyId;
46          _locale = locale;
47          _timeZone = timeZone;
48          _todayString = LanguageUtil.get(_companyId, _locale, "today");
49          _yesterdayString = LanguageUtil.get(_companyId, _locale, "yesterday");
50      }
51  
52      public StringBuffer format(Date date, StringBuffer sb, FieldPosition pos) {
53          String dateString = StringPool.NBSP;
54  
55          if (date != null) {
56              Date today = new Date();
57  
58              Calendar cal = Calendar.getInstance(_timeZone, _locale);
59  
60              cal.setTime(today);
61              cal.add(Calendar.DATE, -1);
62  
63              Date yesterday = cal.getTime();
64  
65              DateFormat dateFormatDate = DateFormats.getDate(_locale, _timeZone);
66              DateFormat dateFormatDateTime = DateFormats.getDateTime(
67                  _locale, _timeZone);
68              DateFormat dateFormatTime = DateFormats.getTime(_locale, _timeZone);
69  
70              dateString = dateFormatDate.format(date);
71  
72              if (dateString.equals(dateFormatDate.format(today))) {
73                  dateString =
74                      _todayString + StringPool.SPACE +
75                          dateFormatTime.format(date);
76              }
77              else if (dateString.equals(dateFormatDate.format(yesterday))) {
78                  dateString =
79                      _yesterdayString + StringPool.SPACE +
80                          dateFormatTime.format(date);
81              }
82              else {
83                  dateString = dateFormatDateTime.format(date);
84              }
85          }
86  
87          return sb.append(dateString);
88      }
89  
90      public Date parse(String source, ParsePosition pos) {
91          DateFormat dateFormatDate = DateFormats.getDate(_locale, _timeZone);
92  
93          DateFormat dateFormatDateTime =
94              DateFormats.getDateTime(_locale, _timeZone);
95  
96          Date today = new Date();
97  
98          String dateString = source.substring(pos.getIndex());
99  
100         if (dateString.startsWith(_todayString)) {
101             dateString.replaceFirst(_todayString, dateFormatDate.format(today));
102         }
103         else if (dateString.startsWith(_yesterdayString)) {
104             Calendar cal = Calendar.getInstance(_timeZone, _locale);
105 
106             cal.setTime(today);
107             cal.add(Calendar.DATE, -1);
108 
109             Date yesterday = cal.getTime();
110 
111             dateString.replaceFirst(
112                 _todayString, dateFormatDate.format(yesterday));
113         }
114 
115         return dateFormatDateTime.parse(dateString, new ParsePosition(0));
116     }
117 
118     private long _companyId;
119     private Locale _locale;
120     private TimeZone _timeZone;
121     private String _todayString;
122     private String _yesterdayString;
123 
124 }