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   * Simple bean to allow date interpolation in the document descriptor, i.e.
30   * <pre>
31   * ${year}  = 2009
32   * ${date}  = 2009-05-17
33   * </pre>
34   * @author ltheussl
35   */
36  public class DateBean
37  {
38      private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
39  
40      private static final SimpleDateFormat YEAR = new SimpleDateFormat( "yyyy", Locale.US );
41      private static final SimpleDateFormat MONTH = new SimpleDateFormat( "MM", Locale.US );
42      private static final SimpleDateFormat DAY = new SimpleDateFormat( "dd", Locale.US );
43      private static final SimpleDateFormat HOUR = new SimpleDateFormat( "HH", Locale.US );
44      private static final SimpleDateFormat MINUTE = new SimpleDateFormat( "mm", Locale.US );
45      private static final SimpleDateFormat SECOND = new SimpleDateFormat( "ss", Locale.US );
46      private static final SimpleDateFormat MILLI_SECOND = new SimpleDateFormat( "SSS", Locale.US );
47      private static final SimpleDateFormat DATE = new SimpleDateFormat( "yyyy-MM-dd", Locale.US );
48      private static final SimpleDateFormat TIME = new SimpleDateFormat( "HH:mm:ss'Z'", Locale.US );
49      private static final SimpleDateFormat DATE_TIME = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US );
50  
51      static
52      {
53          YEAR.setTimeZone( UTC_TIME_ZONE );
54          MONTH.setTimeZone( UTC_TIME_ZONE );
55          DAY.setTimeZone( UTC_TIME_ZONE );
56          HOUR.setTimeZone( UTC_TIME_ZONE );
57          MINUTE.setTimeZone( UTC_TIME_ZONE );
58          SECOND.setTimeZone( UTC_TIME_ZONE );
59          MILLI_SECOND.setTimeZone( UTC_TIME_ZONE );
60          DATE.setTimeZone( UTC_TIME_ZONE );
61          TIME.setTimeZone( UTC_TIME_ZONE );
62          DATE_TIME.setTimeZone( UTC_TIME_ZONE );
63      }
64  
65      private Date date;
66  
67      /**
68       * Construct a new DateBean for the current Date.
69       */
70      public DateBean()
71      {
72          this( new Date() );
73      }
74  
75      /**
76       * Construct a new DateBean with a given Date.
77       *
78       * @param date the date to set.
79       */
80      public DateBean( final Date date )
81      {
82          this.date = date;
83      }
84  
85      /**
86       * Set the Date of this bean.
87       *
88       * @param date the date to set.
89       */
90      public void setDate( final Date date )
91      {
92          this.date = date;
93      }
94  
95      /**
96       * @return the year in format "yyyy".
97       */
98      public String getYear()
99      {
100         synchronized ( this )
101         {
102             return YEAR.format( date );
103         }
104     }
105 
106     /**
107      * @return the month in format "MM".
108      */
109     public String getMonth()
110     {
111         synchronized ( this )
112         {
113             return MONTH.format( date );
114         }
115     }
116 
117     /**
118      * @return the day in format "dd".
119      */
120     public String getDay()
121     {
122         synchronized ( this )
123         {
124             return DAY.format( date );
125         }
126     }
127 
128     /**
129      * @return the hour in format "HH".
130      */
131     public String getHour()
132     {
133         synchronized ( this )
134         {
135             return HOUR.format( date );
136         }
137     }
138 
139     /**
140      * @return the minute in format "mm".
141      */
142     public String getMinute()
143     {
144         synchronized ( this )
145         {
146             return MINUTE.format( date );
147         }
148     }
149 
150     /**
151      * @return the second in format "ss".
152      */
153     public String getSecond()
154     {
155         synchronized ( this )
156         {
157             return SECOND.format( date );
158         }
159     }
160 
161     /**
162      * @return the millisecond in format "SSS".
163      */
164     public String getMillisecond()
165     {
166         synchronized ( this )
167         {
168             return MILLI_SECOND.format( date );
169         }
170     }
171 
172     /**
173      * @return the date using the ISO 8601 format, i.e. <code>yyyy-MM-dd</code>.
174      */
175     public String getDate()
176     {
177         synchronized ( this )
178         {
179             return DATE.format( date );
180         }
181     }
182 
183     /**
184      * @return the time using the ISO 8601 format and UTC time zone, i.e. <code>HH:mm:ss'Z'</code>.
185      */
186     public String getTime()
187     {
188         synchronized ( this )
189         {
190             return TIME.format( date );
191         }
192     }
193 
194     /**
195      * @return the datetime using the ISO 8601 format, i.e. <code>yyyy-MM-dd'T'HH:mm:ss'Z'</code>.
196      */
197     public String getDateTime()
198     {
199         synchronized ( this )
200         {
201             return DATE_TIME.format( date );
202         }
203     }
204 }