com.ms.app.web.commons.tools.DateViewTools.java Source code

Java tutorial

Introduction

Here is the source code for com.ms.app.web.commons.tools.DateViewTools.java

Source

/*
 * Copyright 2011-2016 ZXC.com All right reserved. This software is the confidential and proprietary information of
 * ZXC.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with ZXC.com.
 */
package com.ms.app.web.commons.tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang.time.DurationFormatUtils;

/**
 * vm??
 * 
 * @author zxc Apr 12, 2013 10:58:37 PM
 */
public class DateViewTools {

    /**
     * ?
     */
    private static ThreadLocal<HashMap<String, SimpleDateFormat>> formatHolder = new ThreadLocal<HashMap<String, SimpleDateFormat>>();

    public static final String SIMPLE_DATE_FORMAT_PATTERN = "yyyy-MM-dd";
    public static final String FULL_DATE_FORMAT_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static String formatDate(Date date) {
        if (date == null) {
            return "";
        }
        return getFormat(SIMPLE_DATE_FORMAT_PATTERN).format(date);
    }

    public static String formatFullDate(Date date) {
        if (date == null) {
            return "";
        }
        return getFormat(FULL_DATE_FORMAT_PATTERN).format(date);
    }

    public static String format(Date date, String pattern) {
        if (date == null) {
            return "";
        }
        return getFormat(pattern).format(date);
    }

    public static String formatFullDateToday(Date date) {
        if (date == null) {
            date = new Date(System.currentTimeMillis());
        }
        return getFormat(SIMPLE_DATE_FORMAT_PATTERN).format(date);
    }

    /**
     * cal2?cal1??
     * 
     * <pre>
     * 
     * isExpiredForDays(2011-11-4 2011-11-4 ) 0
     * isExpiredForDays(2011-11-42011-11-5 ) ?0
     * isExpiredForDays(2011-11-52011-11-4 ) 0
     * 
     * </pre>
     */
    public static int compareForDays(Calendar cal1, Calendar cal2) {
        if (isSameDay(cal1, cal2)) {
            return 0;
        }
        return cal1.compareTo(cal2);
    }

    public static int compareForDays(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        return compareForDays(cal1, cal2);
    }

    /**
     * ??,??
     * 
     * <pre>
     * ?currentTime > date 
     * ? 2011-11-4 ,isExpiredForDays(2011-11-4 ) false
     * ? 2011-11-5 ,isExpiredForDays(2011-11-4 ) true
     * 
     * </pre>
     * 
     * @param date ?
     * @return true ?<code>true</code><code>null</code><code>true</code>,<code>false</code>
     */
    public static boolean isExpiredForDays(Date date) {
        if (date == null) {
            return true;
        }
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date);
        if (cal1.compareTo(cal2) > 0) {// ??
            if (isSameDay(cal1, cal2)) {// ??
                return false;
            } else {
                return true;
            }
        } else {// ???
            return false;
        }
    }

    public static boolean isSameDay(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            return false;
        }
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        return isSameDay(cal1, cal2);
    }

    public static boolean isToday(Date date) {
        return date != null && isSameDay(date, new Date());
    }

    public static boolean isSameDay(Calendar cal1, Calendar cal2) {
        return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
                && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)
                && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);
    }

    private static SimpleDateFormat getFormat(String key) {
        HashMap<String, SimpleDateFormat> map = formatHolder.get();
        if (map == null) {
            map = new HashMap<String, SimpleDateFormat>(2);
            formatHolder.set(map);// ?
        }
        SimpleDateFormat simpleDateFormat = map.get(key);
        if (simpleDateFormat == null) {
            simpleDateFormat = new SimpleDateFormat(key);
            map.put(key, simpleDateFormat);
            formatHolder.set(map);// ?
        }
        return simpleDateFormat;
    }

    public static String getDayBefore(int before) {
        Date date = new Date();
        date = DateUtils.addDays(date, -before);
        return getFormat(SIMPLE_DATE_FORMAT_PATTERN).format(date);
    }

    public static String getNow() {
        Date date = new Date();
        return getFormat(SIMPLE_DATE_FORMAT_PATTERN).format(date);
    }

    public static String yesterday() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        return getFormat(SIMPLE_DATE_FORMAT_PATTERN).format(calendar.getTime());
    }

    public static String yesterdayFull() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        return getFormat(FULL_DATE_FORMAT_PATTERN).format(calendar.getTime());
    }

    public static String nextDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        return getFormat(SIMPLE_DATE_FORMAT_PATTERN).format(calendar.getTime());
    }

    public static String nextDayFull() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        return getFormat(FULL_DATE_FORMAT_PATTERN).format(calendar.getTime());
    }

    public static String getNowFull() {
        Date date = new Date(System.currentTimeMillis());
        return getFormat(FULL_DATE_FORMAT_PATTERN).format(date);
    }

    /**
     * ()?Date?<code>null</code>
     */
    public static Date parseFull(String date) {
        try {
            return getFormat(FULL_DATE_FORMAT_PATTERN).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * ()?Date?<code>null</code>
     */
    public static Date parseSimple(String date) {
        try {
            return getFormat(SIMPLE_DATE_FORMAT_PATTERN).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * pattern??
     */
    public static String format(String pattern, Date date) {
        if (StringUtils.isBlank(pattern)) {
            return formatDate(date);
        }
        return getFormat(pattern).format(date);

    }

    /**
     * <p>
     * Duration formatting utilities and constants. The following table describes the tokens used in the pattern
     * language for formatting.
     * </p>
     * <table border="1">
     * <tr>
     * <th>character</th>
     * <th>duration element</th>
     * </tr>
     * <tr>
     * <td>y</td>
     * <td>years</td>
     * </tr>
     * <tr>
     * <td>M</td>
     * <td>months</td>
     * </tr>
     * <tr>
     * <td>d</td>
     * <td>days</td>
     * </tr>
     * <tr>
     * <td>H</td>
     * <td>hours</td>
     * </tr>
     * <tr>
     * <td>m</td>
     * <td>minutes</td>
     * </tr>
     * <tr>
     * <td>s</td>
     * <td>seconds</td>
     * </tr>
     * <tr>
     * <td>S</td>
     * <td>milliseconds</td>
     * </tr>
     * </table>
     */
    public static String formatDuration(Date start, Date end, String format) {
        if (start == null || end == null) {
            return " ";
        }
        long durationMillis = end.getTime() - start.getTime();
        return DurationFormatUtils.formatDuration(durationMillis, format);
    }

    /**
     * 
     * 
     * @param time
     * @return
     */
    public static String getShowText(Date time) {
        if (time == null) {
            return StringUtils.EMPTY;
        }
        long between = Math.abs(System.currentTimeMillis() - time.getTime());
        // 60,
        if (between < 60 * 1000) {
            return "";
        } else {
            long min = (between / 1000 / 60) % 60;
            long hour = (between / 1000 / 60 / 60) % 24;
            long day = between / 1000 / 60 / 60 / 24;
            long month = day / 31;
            if (min > 0 && hour == 0 && day == 0 && month == 0) {
                return min + "?";
            } else if (hour > 0 && day == 0 && month == 0) {
                return hour + "??";
            } else if (day > 0 && month == 0) {
                return day + "?";
            } else if (month > 0) {
                return month + "?";
            }
        }
        return formatFullDate(time);
    }
}