View Javadoc

1   package org.apache.maven.plugins.pdf;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.text.SimpleDateFormat;
23  
24  import java.util.Date;
25  import java.util.Locale;
26  import java.util.TimeZone;
27  
28  
29  /**
30   * Simple bean to allow date interpolation in the document descriptor, i.e.
31   * <pre>
32   * ${year}  = 2009
33   * ${date}  = 2009-05-17
34   * </pre>
35   * @author ltheussl
36   * @version $Id: DateBean.java 787005 2009-06-21 12:54:41Z ltheussl $
37   */
38  public class DateBean
39  {
40      private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
41  
42      private static final SimpleDateFormat YEAR = new SimpleDateFormat( "yyyy", Locale.US );
43      private static final SimpleDateFormat MONTH = new SimpleDateFormat( "MM", Locale.US );
44      private static final SimpleDateFormat DAY = new SimpleDateFormat( "dd", Locale.US );
45      private static final SimpleDateFormat HOUR = new SimpleDateFormat( "HH", Locale.US );
46      private static final SimpleDateFormat MINUTE = new SimpleDateFormat( "mm", Locale.US );
47      private static final SimpleDateFormat SECOND = new SimpleDateFormat( "ss", Locale.US );
48      private static final SimpleDateFormat MILLI_SECOND = new SimpleDateFormat( "SSS", Locale.US );
49      private static final SimpleDateFormat DATE = new SimpleDateFormat( "yyyy-MM-dd", Locale.US );
50      private static final SimpleDateFormat TIME = new SimpleDateFormat( "HH:mm:ss\'Z\'", Locale.US );
51      private static final SimpleDateFormat DATE_TIME = new SimpleDateFormat( "yyyy-MM-dd\'T\'HH:mm:ss\'Z\'", Locale.US );
52  
53      static
54      {
55          YEAR.setTimeZone( UTC_TIME_ZONE );
56          MONTH.setTimeZone( UTC_TIME_ZONE );
57          DAY.setTimeZone( UTC_TIME_ZONE );
58          HOUR.setTimeZone( UTC_TIME_ZONE );
59          MINUTE.setTimeZone( UTC_TIME_ZONE );
60          SECOND.setTimeZone( UTC_TIME_ZONE );
61          MILLI_SECOND.setTimeZone( UTC_TIME_ZONE );
62          DATE.setTimeZone( UTC_TIME_ZONE );
63          TIME.setTimeZone( UTC_TIME_ZONE );
64          DATE_TIME.setTimeZone( UTC_TIME_ZONE );
65      }
66  
67      private Date date;
68  
69      /**
70       * Construct a new DateBean for the current Date.
71       */
72      public DateBean()
73      {
74          this( new Date() );
75      }
76  
77      /**
78       * Construct a new DateBean with a given Date.
79       *
80       * @param date the date to set.
81       */
82      public DateBean( final Date date )
83      {
84          this.date = date;
85      }
86  
87      /**
88       * Set the Date of this bean.
89       *
90       * @param date the date to set.
91       */
92      public void setDate( final Date date )
93      {
94          this.date = date;
95      }
96  
97  
98      /**
99       * @return the year in format "yyyy".
100      */
101     public String getYear()
102     {
103         synchronized ( this )
104         {
105             return YEAR.format( date );
106         }
107     }
108 
109     /**
110      * @return the month in format "MM".
111      */
112     public String getMonth()
113     {
114         synchronized ( this )
115         {
116             return MONTH.format( date );
117         }
118     }
119 
120     /**
121      * @return the day in format "dd".
122      */
123     public String getDay()
124     {
125         synchronized ( this )
126         {
127             return DAY.format( date );
128         }
129     }
130 
131     /**
132      * @return the hour in format "HH".
133      */
134     public String getHour()
135     {
136         synchronized ( this )
137         {
138             return HOUR.format( date );
139         }
140     }
141 
142     /**
143      * @return the minute in format "mm".
144      */
145     public String getMinute()
146     {
147         synchronized ( this )
148         {
149             return MINUTE.format( date );
150         }
151     }
152 
153     /**
154      * @return the second in format "ss".
155      */
156     public String getSecond()
157     {
158         synchronized ( this )
159         {
160             return SECOND.format( date );
161         }
162     }
163 
164     /**
165      * @return the millisecond in format "SSS".
166      */
167     public String getMillisecond()
168     {
169         synchronized ( this )
170         {
171             return MILLI_SECOND.format( date );
172         }
173     }
174 
175     /**
176      * @return the date using the ISO 8601 format, i.e. <code>yyyy-MM-dd</code>.
177      */
178     public String getDate()
179     {
180         synchronized ( this )
181         {
182             return DATE.format( date );
183         }
184     }
185 
186     /**
187      * @return the time using the ISO 8601 format and UTC time zone, i.e. <code>HH:mm:ss'Z'</code>.
188      */
189     public String getTime()
190     {
191         synchronized ( this )
192         {
193             return TIME.format( date );
194         }
195     }
196 
197     /**
198      * @return the datetime using the ISO 8601 format, i.e. <code>yyyy-MM-dd'T'HH:mm:ss'Z'</code>.
199      */
200     public String getDateTime()
201     {
202         synchronized ( this )
203         {
204             return DATE_TIME.format( date );
205         }
206     }
207 }