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    *      http://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   */
47  final class DateStringLookup extends AbstractStringLookup {
48  
49      /**
50       * Defines the singleton for this class.
51       */
52      static final DateStringLookup INSTANCE = new DateStringLookup();
53  
54      /**
55       * No need to build instances for now.
56       */
57      private DateStringLookup() {
58          // empty
59      }
60  
61      /**
62       * Formats the given {@code date} long with the given {@code format}.
63       *
64       * @param dateMillis the date to format
65       * @param format the format string for {@link SimpleDateFormat}.
66       * @return The formatted date
67       */
68      private String formatDate(final long dateMillis, final String format) {
69          FastDateFormat dateFormat = null;
70          if (format != null) {
71              try {
72                  dateFormat = FastDateFormat.getInstance(format);
73              } catch (final Exception ex) {
74                  throw IllegalArgumentExceptions.format(ex, "Invalid date format: [%s]", format);
75              }
76          }
77          if (dateFormat == null) {
78              dateFormat = FastDateFormat.getInstance();
79          }
80          return dateFormat.format(new Date(dateMillis));
81      }
82  
83      /**
84       * Formats the current date with the format given in the key in a format compatible with
85       * {@link java.text.SimpleDateFormat}.
86       *
87       * @param key the format to use. If null, the default {@link DateFormat} will be used.
88       * @return The value of the environment variable.
89       */
90      @Override
91      public String lookup(final String key) {
92          return formatDate(System.currentTimeMillis(), key);
93      }
94  }