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