Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DateFormatter |
|
| 1.5;1,5 |
1 | package org.apache.turbine.services.pull.util; | |
2 | ||
3 | ||
4 | /* | |
5 | * Licensed to the Apache Software Foundation (ASF) under one | |
6 | * or more contributor license agreements. See the NOTICE file | |
7 | * distributed with this work for additional information | |
8 | * regarding copyright ownership. The ASF licenses this file | |
9 | * to you under the Apache License, Version 2.0 (the | |
10 | * "License"); you may not use this file except in compliance | |
11 | * with the License. You may obtain a copy of the License at | |
12 | * | |
13 | * http://www.apache.org/licenses/LICENSE-2.0 | |
14 | * | |
15 | * Unless required by applicable law or agreed to in writing, | |
16 | * software distributed under the License is distributed on an | |
17 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
18 | * KIND, either express or implied. See the License for the | |
19 | * specific language governing permissions and limitations | |
20 | * under the License. | |
21 | */ | |
22 | ||
23 | ||
24 | import java.text.SimpleDateFormat; | |
25 | import java.util.Date; | |
26 | ||
27 | import org.apache.commons.lang.StringUtils; | |
28 | import org.apache.turbine.Turbine; | |
29 | import org.apache.turbine.services.pull.ApplicationTool; | |
30 | ||
31 | /** | |
32 | * This pull tool is used to format date objects into strings. | |
33 | * | |
34 | * @author <a href="mailto:qmccombs@nequalsone.com">Quinton McCombs</a> | |
35 | * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a> | |
36 | * @version $Id: DateFormatter.java 1773378 2016-12-09 13:19:59Z tv $ | |
37 | */ | |
38 | 12 | public class DateFormatter |
39 | implements ApplicationTool | |
40 | { | |
41 | /** Default date format */ | |
42 | private static final String DATE_FORMAT_DEFAULT = "MM/dd/yyyy"; | |
43 | ||
44 | /** | |
45 | * Property tag for the date format that is to be used for the web | |
46 | * application. | |
47 | */ | |
48 | private static final String DATE_FORMAT_KEY = "tool.dateTool.format"; | |
49 | ||
50 | 12 | private String dateFormat = null; |
51 | ||
52 | /** | |
53 | * Initialize the application tool. The data parameter holds a different | |
54 | * type depending on how the tool is being instantiated: | |
55 | * <ul> | |
56 | * <li>For global tools data will be null</li> | |
57 | * <li>For request tools data will be of type RunData</li> | |
58 | * <li>For session and persistent tools data will be of type User</li> | |
59 | * </ul> | |
60 | * | |
61 | * @param data initialization data | |
62 | */ | |
63 | @Override | |
64 | public void init(Object data) | |
65 | { | |
66 | 9 | dateFormat = Turbine.getConfiguration() |
67 | .getString(DATE_FORMAT_KEY, DATE_FORMAT_DEFAULT); | |
68 | 9 | } |
69 | ||
70 | /** | |
71 | * Refresh the application tool. This is | |
72 | * necessary for development work where you | |
73 | * probably want the tool to refresh itself | |
74 | * if it is using configuration information | |
75 | * that is typically cached after initialization | |
76 | */ | |
77 | @Override | |
78 | public void refresh() | |
79 | { | |
80 | // empty | |
81 | 0 | } |
82 | ||
83 | /** | |
84 | * Formats the given date as a String using the default date format. | |
85 | * The default date format is MM/dd/yyyy | |
86 | * | |
87 | * @param theDate date to format | |
88 | * @return String value of the date | |
89 | */ | |
90 | public String format(Date theDate) | |
91 | { | |
92 | 0 | return format(theDate, dateFormat); |
93 | } | |
94 | ||
95 | /** | |
96 | * Formats the given date as a String. | |
97 | * | |
98 | * @param theDate date to format | |
99 | * @param dateFormatString format string to use. See java.text.SimpleDateFormat | |
100 | * for details. | |
101 | * @return String value of the date | |
102 | */ | |
103 | public String format(Date theDate, String dateFormatString) | |
104 | { | |
105 | 5 | String result = null; |
106 | 5 | SimpleDateFormat sdf = new SimpleDateFormat(); |
107 | ||
108 | 5 | if (StringUtils.isEmpty(dateFormatString) || theDate == null) |
109 | { | |
110 | 3 | result = ""; |
111 | } | |
112 | else | |
113 | { | |
114 | 2 | sdf.applyPattern(dateFormatString); |
115 | 2 | result = sdf.format(theDate); |
116 | } | |
117 | 5 | return result; |
118 | } | |
119 | } |