DateStringLookup.java

  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. import java.text.DateFormat;
  19. import java.text.SimpleDateFormat;
  20. import java.util.Date;

  21. import org.apache.commons.lang3.time.FastDateFormat;
  22. import org.apache.commons.text.StringSubstitutor;

  23. /**
  24.  * Formats the current date with the format given in the key in a format compatible with
  25.  * {@link java.text.SimpleDateFormat}.
  26.  * <p>
  27.  * Using a {@link StringLookup} from the {@link StringLookupFactory}:
  28.  * </p>
  29.  *
  30.  * <pre>
  31.  * StringLookupFactory.INSTANCE.dateStringLookup().lookup("yyyy-MM-dd");
  32.  * </pre>
  33.  * <p>
  34.  * Using a {@link StringSubstitutor}:
  35.  * </p>
  36.  *
  37.  * <pre>
  38.  * StringSubstitutor.createInterpolator().replace("... ${date:yyyy-MM-dd} ..."));
  39.  * </pre>
  40.  * <p>
  41.  * The above examples convert {@code "yyyy-MM-dd"} to today's date, for example, {@code "2019-08-04"}.
  42.  * </p>
  43.  */
  44. final class DateStringLookup extends AbstractStringLookup {

  45.     /**
  46.      * Defines the singleton for this class.
  47.      */
  48.     static final DateStringLookup INSTANCE = new DateStringLookup();

  49.     /**
  50.      * No need to build instances for now.
  51.      */
  52.     private DateStringLookup() {
  53.         // empty
  54.     }

  55.     /**
  56.      * Formats the given {@code date} long with the given {@code format}.
  57.      *
  58.      * @param dateMillis the date to format
  59.      * @param format the format string for {@link SimpleDateFormat}.
  60.      * @return The formatted date
  61.      */
  62.     private String formatDate(final long dateMillis, final String format) {
  63.         FastDateFormat dateFormat = null;
  64.         if (format != null) {
  65.             try {
  66.                 dateFormat = FastDateFormat.getInstance(format);
  67.             } catch (final Exception ex) {
  68.                 throw IllegalArgumentExceptions.format(ex, "Invalid date format: [%s]", format);
  69.             }
  70.         }
  71.         if (dateFormat == null) {
  72.             dateFormat = FastDateFormat.getInstance();
  73.         }
  74.         return dateFormat.format(new Date(dateMillis));
  75.     }

  76.     /**
  77.      * Formats the current date with the format given in the key in a format compatible with
  78.      * {@link java.text.SimpleDateFormat}.
  79.      *
  80.      * @param key the format to use. If null, the default {@link DateFormat} will be used.
  81.      * @return The value of the environment variable.
  82.      */
  83.     @Override
  84.     public String lookup(final String key) {
  85.         return formatDate(System.currentTimeMillis(), key);
  86.     }
  87. }