1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.lang3.time;
18
19 import java.text.FieldPosition;
20 import java.util.Calendar;
21 import java.util.Date;
22 import java.util.GregorianCalendar;
23 import java.util.Locale;
24 import java.util.TimeZone;
25
26 /**
27 * DatePrinter is the "missing" interface for the format methods of
28 * {@link java.text.DateFormat}. You can obtain an object implementing this
29 * interface by using one of the FastDateFormat factory methods.
30 * <p>
31 * Warning: Since binary compatible methods may be added to this interface in any
32 * release, developers are not expected to implement this interface.
33 *
34 * @since 3.2
35 */
36 public interface DatePrinter {
37
38 /**
39 * Formats a {@link Calendar} object.
40 * The TimeZone set on the Calendar is only used to adjust the time offset.
41 * The TimeZone specified during the construction of the Parser will determine the TimeZone
42 * used in the formatted string.
43 *
44 * @param calendar the calendar to format.
45 * @return the formatted string.
46 */
47 String format(Calendar calendar);
48
49 /**
50 * Formats a {@link Calendar} object into the supplied {@link Appendable}.
51 * The TimeZone set on the Calendar is only used to adjust the time offset.
52 * The TimeZone specified during the construction of the Parser will determine the TimeZone
53 * used in the formatted string.
54 *
55 * @param calendar the calendar to format.
56 * @param buf the buffer to format into.
57 * @param <B> the Appendable class type, usually StringBuilder or StringBuffer.
58 * @return the specified string buffer.
59 * @since 3.5
60 */
61 <B extends Appendable> B format(Calendar calendar, B buf);
62
63 /**
64 * Formats a {@link Calendar} object into the supplied {@link StringBuffer}.
65 * The TimeZone set on the Calendar is only used to adjust the time offset.
66 * The TimeZone specified during the construction of the Parser will determine the TimeZone
67 * used in the formatted string.
68 *
69 * @param calendar the calendar to format.
70 * @param buf the buffer to format into.
71 * @return the specified string buffer.
72 * @deprecated Use {{@link #format(Calendar, Appendable)}.
73 */
74 @Deprecated
75 StringBuffer format(Calendar calendar, StringBuffer buf);
76
77 /**
78 * Formats a {@link Date} object using a {@link GregorianCalendar}.
79 *
80 * @param date the date to format
81 * @return the formatted string
82 */
83 String format(Date date);
84
85 /**
86 * Formats a {@link Date} object into the
87 * supplied {@link Appendable} using a {@link GregorianCalendar}.
88 *
89 * @param date the date to format.
90 * @param buf the buffer to format into.
91 * @param <B> the Appendable class type, usually StringBuilder or StringBuffer.
92 * @return the specified string buffer.
93 * @since 3.5
94 */
95 <B extends Appendable> B format(Date date, B buf);
96
97 /**
98 * Formats a {@link Date} object into the
99 * supplied {@link StringBuffer} using a {@link GregorianCalendar}.
100 *
101 * @param date the date to format.
102 * @param buf the buffer to format into.
103 * @return the specified string buffer.
104 * @deprecated Use {{@link #format(Date, Appendable)}.
105 */
106 @Deprecated
107 StringBuffer format(Date date, StringBuffer buf);
108
109 /**
110 * Formats a millisecond {@code long} value.
111 *
112 * @param millis the millisecond value to format.
113 * @return the formatted string.
114 * @since 2.1
115 */
116 String format(long millis);
117
118 /**
119 * Formats a millisecond {@code long} value into the
120 * supplied {@link Appendable}.
121 *
122 * @param millis the millisecond value to format.
123 * @param buf the buffer to format into.
124 * @param <B> the Appendable class type, usually StringBuilder or StringBuffer.
125 * @return the specified string buffer.
126 * @since 3.5
127 */
128 <B extends Appendable> B format(long millis, B buf);
129
130 /**
131 * Formats a millisecond {@code long} value into the
132 * supplied {@link StringBuffer}.
133 *
134 * @param millis the millisecond value to format.
135 * @param buf the buffer to format into.
136 * @return the specified string buffer.
137 * @deprecated Use {{@link #format(long, Appendable)}.
138 */
139 @Deprecated
140 StringBuffer format(long millis, StringBuffer buf);
141
142 /**
143 * Formats a {@link Date}, {@link Calendar} or
144 * {@link Long} (milliseconds) object.
145 *
146 * @param obj the object to format.
147 * @param toAppendTo the buffer to append to.
148 * @param pos the position - ignored.
149 * @return the buffer passed in.
150 * @see java.text.DateFormat#format(Object, StringBuffer, FieldPosition)
151 */
152 StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos);
153
154 /**
155 * Gets the locale used by this printer.
156 *
157 * @return the locale.
158 */
159 Locale getLocale();
160
161 // Accessors
162 /**
163 * Gets the pattern used by this printer.
164 *
165 * @return the pattern, {@link java.text.SimpleDateFormat} compatible.
166 */
167 String getPattern();
168
169 /**
170 * Gets the time zone used by this printer.
171 *
172 * <p>This zone is always used for {@link Date} printing.</p>
173 *
174 * @return the time zone.
175 */
176 TimeZone getTimeZone();
177 }