⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utilities.java

📁 A Java web application, based on Struts and Hibernate, that serves as an online running log. Users m
💻 JAVA
字号:
/* @LICENSE_COPYRIGHT@ */
package net.sf.irunninglog.util;

import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.GregorianCalendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Generic utility class for the application.  This class contains various
 * static utility methods useful for performing common tasks.
 *
 * @author <a href="mailto:allan_e_lewis@yahoo.com">Allan Lewis</a>
 * @version $Revision: 1.1.1.1 $ $Date: 2005/06/23 01:49:04 $
 * @since iRunningLog 1.0
 */
public final class Utilities {

    /** <code>Log</code> instance for this class. */
    private static final Log LOG = LogFactory.getLog(Utilities.class);
    /** Number of years used to adjust for the epoch. */
    private static final int EPOCH_ADJUSTMENT = 1970;
    /** Number of milliseconds in one hour. */
    private static final int MILLIS_IN_HOUR = 3600000;
    /** Number of milliseconds in one minute. */
    private static final int MILLIS_IN_MINUTE = 60000;
    /** Number of milliseconds in one second. */
    private static final int MILLIS_IN_SECOND = 1000;

    /** Utility class - does not expose a constructor. */
    private Utilities() { }

    /**
     * Determine whether or not a <code>String</code> is blank.  A blank
     * <code>String</code> is one that is either <code>null</code>, or whose
     * length (after trimming) is zero.
     *
     * @param value The <code>String</code> to be checked
     * @return True if the <code>String</code> is blank, false otherwise
     */
    public static boolean isBlank(String value) {
        return value == null || value.trim().length() == 0;
    }

    /**
     * Get a representation of an <code>HttpRequest</code> as a <code>
     * String</code>.  This will convert the important information from the
     * request into a string representation.
     *
     * @param request The request to be represented as a string
     * @return The string representation of the request
     */
    public static String toString(HttpServletRequest request) {
        if (request == null) {
            return null;
        }

        StringBuffer buff = new StringBuffer();
        Enumeration e = null;

        buff.append(request.getClass());
        buff.append("[");

        buff.append("user='");
        if (request.getUserPrincipal() == null) {
            buff.append(request.getUserPrincipal());
        } else {
            buff.append(request.getUserPrincipal().getName());
        }
        buff.append("' ");

        buff.append("requestURL='");
        buff.append(request.getRequestURL());
        buff.append("' ");
        buff.append("method='");
        buff.append(request.getMethod());
        buff.append("' ");
        buff.append("parameters={");

        e = request.getParameterNames();
        while (e != null && e.hasMoreElements()) {
            String name = (String) e.nextElement();
            buff.append(name);
            buff.append("='");
            buff.append(request.getParameter(name));
            buff.append("'");
            if (e.hasMoreElements()) {
                buff.append(", ");
            }
        }

        buff.append("} ");
        buff.append("attributes={");

        e = request.getAttributeNames();
        while (e != null && e.hasMoreElements()) {
            String name = (String) e.nextElement();
            buff.append(name);
            buff.append("='");
            buff.append(request.getAttribute(name));
            buff.append("'");
            if (e.hasMoreElements()) {
                buff.append(", ");
            }
        }

        buff.append("}");
        buff.append("]");

        return buff.toString();
    }

    /**
     * Get a representation of an <code>HttpSession</code> as a <code>
     * String</code>.  This will convert the important information from the
     * session into a string representation.
     *
     * @param session The session to be represented as a string
     * @return The string representation of the session
     */
    public static String toString(HttpSession session) {
        if (session == null) {
            return null;
        }

        StringBuffer buff = new StringBuffer();

        buff.append(session.getClass());
        buff.append("[");
        buff.append("id='");
        buff.append(session.getId());
        buff.append("' ");
        buff.append("attributes={");

        Enumeration e = session.getAttributeNames();
        while (e != null && e.hasMoreElements()) {
            String name = (String) e.nextElement();
            buff.append(name);
            buff.append("='");
            buff.append(session.getAttribute(name));
            buff.append("'");
            if (e.hasMoreElements()) {
                buff.append(", ");
            }
        }

        buff.append("}");
        buff.append("]");

        return buff.toString();
    }

    /**
     * Get the time difference in years between two dates.  The first date must
     * not be before the second date.
     *
     * @param larger The larger of the two dates.  Must be non-null
     * @param smaller The smaller of the two dates.  Must be non-null and not
     *                after the larger date.
     * @return The time difference (in years) between the two dates.
     */
    public static int getTimeDiffInYears(Date larger, Date smaller) {
        if (larger == null || smaller == null) {
            throw new NullPointerException("Neither date may be null");
        }

        if (smaller.after(larger)) {
            throw new IllegalArgumentException("The 'smaller' date cannot be"
                                               + " after the 'larger' date");
        }

        if (LOG.isDebugEnabled()) {
            LOG.debug("getTimeDiffInYears: Calculating the difference"
                      + " (in years) between " + larger + " and " + smaller);
        }

        long timeDiff = larger.getTime() - smaller.getTime();

        if (LOG.isDebugEnabled()) {
            LOG.debug("getTimeDiffInYears: Time difference is "
                      + timeDiff + "ms");
        }

        // Create a calendar and populate its time with the time difference.
        // Then, subtract off the epoch.
        Calendar cal = GregorianCalendar.getInstance();
        cal.set(Calendar.YEAR, EPOCH_ADJUSTMENT);
        cal.set(Calendar.MONTH, Calendar.JANUARY);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.setTimeInMillis(cal.getTimeInMillis() + timeDiff);

        int year = cal.get(Calendar.YEAR);
        int diffInYears = year - EPOCH_ADJUSTMENT;

        if (LOG.isDebugEnabled()) {
            LOG.debug("getTimeDiffInYears: Time difference in years is "
                      + diffInYears);
        }

        return diffInYears;
    }

    /**
     * Return the number of milliseconds in a given time specified in hours,
     * minutes, seconds, and milliseconds.  This will convert the necessary
     * units into milliseconds, add them, and return the results.
     *
     * @param hours The number of hours in the time
     * @param minutes The number of minutes in the time
     * @param seconds The number of seconds in the time
     * @param milliseconds The number of milliseconds in the time
     * @return The time, represented as milliseconds
     */
    public static long getMilliseconds(long hours, long minutes,
                                       long seconds, long milliseconds) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("getMilliseconds: hours - " + hours);
            LOG.debug("getMilliseconds: minutes - " + minutes);
            LOG.debug("getMilliseconds: seconds - " + seconds);
            LOG.debug("getMilliseconds: milliseconds - " + milliseconds);
        }

        return (hours * MILLIS_IN_HOUR)
             + (minutes * MILLIS_IN_MINUTE)
             + (seconds * MILLIS_IN_SECOND)
             + milliseconds;
    }

}

⌨️ 快捷键说明

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