View Javadoc

1   /*
2    *  DISCLAIMER
3    */
4   package org.flowfuse.base.frontend.support.displaytag;
5   
6   import org.apache.commons.logging.Log;
7   import org.apache.commons.logging.LogFactory;
8   import org.displaytag.decorator.ColumnDecorator;
9   import org.displaytag.exception.DecoratorException;
10  
11  import java.text.DateFormat;
12  import java.util.Calendar;
13  import java.util.Date;
14  
15  /***
16   * <p>The <code>ExtendedDateFormatter</code> formats dates to a more user
17   * friendly format and displays concurrent dates as yesterday, today, and
18   * tomorrow.
19   *
20   * @author <a href="stefan@flowfuse.org">Stefan Kleineikenscheidt</a>,
21   *         Flowfuse.org
22   * @since 1.0
23   * @version $Id: ExtendedDateFormatter.java,v 1.1 2005/11/20 15:02:11 skleinei Exp $
24   */
25  public class ExtendedDateFormatter implements ColumnDecorator {
26  
27    /***
28     * RCS ID
29     */
30    public final static String rcsid = "$Id: ExtendedDateFormatter.java,v 1.1 2005/11/20 15:02:11 skleinei Exp $";
31  
32    /***
33     * Logger
34     */
35    protected final Log logger = LogFactory.getLog(this.getClass());
36  
37    private DateFormat dateFormat = null;
38  
39    private Date yesterday0000 = null;
40  
41    private Date today0000 = null;
42  
43    private Date tomorrow0000 = null;
44  
45    private Date dayAfterTomorrow0000 = null;
46  
47    public ExtendedDateFormatter() {
48      this.dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
49  
50      // create Calendar which is set to yesterday 00:00 and
51      // initialize this.yesterday0000
52      Calendar rightNow = Calendar.getInstance();
53      Calendar cal = Calendar.getInstance();
54      cal.clear();
55      cal.set(rightNow.get(Calendar.YEAR),
56              rightNow.get(Calendar.MONTH),
57              rightNow.get(Calendar.DATE) );
58      cal.roll(Calendar.DATE, false);
59      this.yesterday0000 = cal.getTime();
60  
61      // Roll it to today 00:00 and initalize this.today0000
62      cal.roll(Calendar.DATE, true);
63      this.today0000 = cal.getTime();
64  
65      // Roll it to tomorrow 00:00 and initalize this.tomorrow0000
66      cal.roll(Calendar.DATE, true);
67      this.tomorrow0000 = cal.getTime();
68  
69      // Roll it to the day after tomorrow 00:00
70      // and initalize this.dayAfterTomorrow0000
71      cal.roll(Calendar.DATE, true);
72      this.dayAfterTomorrow0000 = cal.getTime();
73  
74    }
75  
76    public String decorate(Object o) throws DecoratorException {
77      if (o == null) {
78        logger.warn("No date for work item found to be displayed. " +
79                "This can indicate that the database is hosed.");
80        return "--";
81      } else if (! (o instanceof Date)) {
82        logger.warn("No valid date for work item found to be displayed. " +
83                "Please check with your developer.");
84        return o.toString();
85      }
86  
87      Date date = (Date) o;
88  
89      if (date.after(yesterday0000) && date.before(today0000)) {
90        return "yesterday";
91      } else if (date.after(yesterday0000) && date.before(tomorrow0000)) {
92        return "today";
93      } else if (date.after(today0000) && date.before(dayAfterTomorrow0000)) {
94        return "tomorrow";
95      } else {
96        return this.dateFormat.format(date);
97      }
98    }
99  }
100 
101 /* EOF */