View Javadoc
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.text.lookup;
18  
19  import java.text.DateFormat;
20  import java.text.SimpleDateFormat;
21  import java.util.Date;
22  
23  import org.apache.commons.lang3.time.FastDateFormat;
24  import org.apache.commons.text.StringSubstitutor;
25  
26  /**
27   * Formats the current date with the format given in the key in a format compatible with
28   * {@link java.text.SimpleDateFormat}.
29   * <p>
30   * Using a {@link StringLookup} from the {@link StringLookupFactory}:
31   * </p>
32   *
33   * <pre>
34   * StringLookupFactory.INSTANCE.dateStringLookup().lookup("yyyy-MM-dd");
35   * </pre>
36   * <p>
37   * Using a {@link StringSubstitutor}:
38   * </p>
39   *
40   * <pre>
41   * StringSubstitutor.createInterpolator().replace("... ${date:yyyy-MM-dd} ..."));
42   * </pre>
43   * <p>
44   * The above examples convert {@code "yyyy-MM-dd"} to today's date, for example, {@code "2019-08-04"}.
45   * </p>
46   * <p>
47   * Public access is through {@link StringLookupFactory}.
48   * </p>
49   *
50   * @see StringLookupFactory
51   */
52  final class DateStringLookup extends AbstractStringLookup {
53  
54      /**
55       * Defines the singleton for this class.
56       */
57      static final DateStringLookup INSTANCE = new DateStringLookup();
58  
59      /**
60       * No need to build instances for now.
61       */
62      private DateStringLookup() {
63          // empty
64      }
65  
66      /**
67       * Formats the given {@code date} long with the given {@code format}.
68       *
69       * @param dateMillis the date to format.
70       * @param format the format string for {@link SimpleDateFormat}.
71       * @return The formatted date.
72       */
73      private String formatDate(final long dateMillis, final String format) {
74          FastDateFormat dateFormat = null;
75          if (format != null) {
76              try {
77                  dateFormat = FastDateFormat.getInstance(format);
78              } catch (final Exception ex) {
79                  throw IllegalArgumentExceptions.format(ex, "Invalid date format: [%s]", format);
80              }
81          }
82          if (dateFormat == null) {
83              dateFormat = FastDateFormat.getInstance();
84          }
85          return dateFormat.format(new Date(dateMillis));
86      }
87  
88      /**
89       * Formats the current date with the format given in the key in a format compatible with
90       * {@link java.text.SimpleDateFormat}.
91       *
92       * @param key the format to use. If null, the default {@link DateFormat} will be used.
93       * @return The value of the environment variable.
94       */
95      @Override
96      public String lookup(final String key) {
97          return formatDate(System.currentTimeMillis(), key);
98      }
99  }