lazydate.java

来自「这是一个mvc模式」· Java 代码 · 共 92 行

JAVA
92
字号
package jsp.tags.dapact.util;

import java.util.*;

/**
 * Title:        Data Aware Processing And Control Tags
 * Description:  Tag library for the processing and controlling the input and output of data.
 * Copyright:
 * Compile Date: @compile_date@
 * @author Dov Bulka - Java Performance and Scalability Volume 1: Server-Side Programming Techniques.
 * @amp_sign@version @VERSION@
 */

/**
 * Class only processes a data into a string every refresh (in milliseconds) - even
 * though this will be used by multiple threads, nothing will be synchronized. This means
 * that the member data may not be updated correctly, so you should set the refresh
 * rate low enough to minimize any problems this might cause you. If you need exact time,
 * use the Date class directly.
 *
 * @author Dov Bulka - Java Performance and Scalability Volume 1: Server-Side Programming Techniques.
 */
public class LazyDate
{
  /**
   * The default number of milliseconds to refresh the string representation of the
   * date and time.
   */
  public static final long DEFAULT_LAZY_DATE_REFRESH = 1000;

  /**
   * Constructor that allows specification of how often to refresh the date.
   *
   * @param refresh how many milliseconds between refreshing.
   */
  public LazyDate(long refresh)
  {
    cacheRefresh = refresh;
    update();
  }

  /**
   * Retrieve a shared instance (creates one version and does not create another
   * into resetShared() is called).
   *
   * @return returns a shared instance of this class.
   */
  public static final LazyDate getInstance()
  {
    return lazyDate;
  }

  /**
   * Retrieve today's date as a string.
   */
  public String todaysDate()
  {
    long now = System.currentTimeMillis();

    if ((now-lastCheck) > cacheRefresh)
    {
       update();
    }
    return today;
  }

  /**
   * Retrieve today's date as a string with a space a the end.
   */
  public String todaysDateWithSpace()
  {
    todaysDate();
    return todayWithSpace;
  }

  /**
   * Update the string representation of the date.
   */
  private void update()
  {
    lastCheck = System.currentTimeMillis();
    today = (new Date()).toString();
    todayWithSpace = today + " ";
  }

  private long lastCheck = 0; // Never checked before
  private String today;
  private String todayWithSpace;
  private long cacheRefresh;
  private static LazyDate lazyDate = new LazyDate(DEFAULT_LAZY_DATE_REFRESH);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?