时间工具类

时间工具类
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Set;
import java.util.TimeZone;
import java.util.TreeSet;

public class DateUtils {
    // 格式化时间
    public static final SimpleDateFormat TIME_ORDER = new SimpleDateFormat(
            "yyyyMMddHHmmssSSS");
    public static final SimpleDateFormat TIME_TMDHMS = new SimpleDateFormat(
            "yyyyMMddHHmmss");
    public static final SimpleDateFormat TIME_NORMAL = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat TIME_NORMAL_MS = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm");
    public static final SimpleDateFormat TIME_YMD = new SimpleDateFormat(
            "yyyy-MM-dd");
    public static final SimpleDateFormat TIME_TODAY = new SimpleDateFormat(
            "yyyyMMdd");
    public static final SimpleDateFormat TIME_YEAR = new SimpleDateFormat(
            "yyyy");
    public static final SimpleDateFormat TIME_MONTH = new SimpleDateFormat("MM");
    public static final SimpleDateFormat TIME_DATE = new SimpleDateFormat("dd");
    public static final SimpleDateFormat TIME_HOUR = new SimpleDateFormat("HH");
    public static final SimpleDateFormat TIME_MINUTE = new SimpleDateFormat(
            "mm");
    public static final SimpleDateFormat TIME_SECOND = new SimpleDateFormat(
            "ss");
    public static final SimpleDateFormat TIME_YM = new SimpleDateFormat(
            "yyyy-MM");
    public static final SimpleDateFormat TIMEYM = new SimpleDateFormat("yyyyMM");
    public static final SimpleDateFormat TIME_HMS = new SimpleDateFormat(
            "HH:mm:ss");
    public static final SimpleDateFormat TIME_YMD_CN = new SimpleDateFormat(
            "yyyy年MM月dd日");
    public static final SimpleDateFormat TIME_HM = new SimpleDateFormat("HH:mm");

    /**
     * 获取当前时间字符串(yyyy-MM-dd HH:mm:ss)
     */
    public static String getNormalTime() {
        return TIME_NORMAL.format(System.currentTimeMillis());
    }

    public static String getNormalTime(long time) {
        return TIME_NORMAL.format(time);
    }

    public static Date getNormalTime(String time) {
        try {
            return TIME_NORMAL.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取当前时间字符串(yyyyMMddHHmmssSSS)
     */
    public static String getOrderTime() {
        return TIME_ORDER.format(System.currentTimeMillis());
    }

    public static String getOrderTime(long time) {
        return TIME_ORDER.format(time);
    }

    /**
     * 获取当前时间字符串不含毫秒数(yyyyMMddHHmmss)
     */
    public static String getNowTime() {
        return TIME_TMDHMS.format(System.currentTimeMillis());
    }

    public static String getNowTime(long time) {
        return TIME_TMDHMS.format(time);
    }

    /**
     * 获取当前时间字符串(yyyy-MM-dd)
     */
    public static String getYmdTime() {
        return TIME_YMD.format(System.currentTimeMillis());
    }

    public static String getYmdTime(long time) {
        return TIME_YMD.format(time);
    }

    public static Date getYmdTime(String time) {
        try {
            return TIME_YMD.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取当前月份字符串(yyyy-MM)
     */
    public static String getYmTime() {
        return TIME_YM.format(System.currentTimeMillis());
    }

    public static String getYmTime(long time) {
        return TIME_YM.format(time);
    }

    public static Date getYmTime(String time) {
        try {
            return TIME_YM.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取当前时间字符串(yyyyMMdd)
     */
    public static String getTodayTime() {
        return TIME_TODAY.format(System.currentTimeMillis());
    }

    public static String getTodayTime(long time) {
        return TIME_TODAY.format(time);
    }

    /**
     * 获取昨天时期字符串(yyyy-MM-dd)
     */
    public static String getYestoday() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        return TIME_YMD.format(cal.getTime());
    }

    public static String getYestoday(long time) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(time);
        cal.add(Calendar.DATE, -1);
        return TIME_YMD.format(cal.getTime());
    }

    /**
     * 获取当前时间年份数字
     */
    public static int getYear() {
        return Integer.parseInt(TIME_YEAR.format(System.currentTimeMillis()));
    }

    public static int getYear(long time) {
        return Integer.parseInt(TIME_YEAR.format(time));
    }

    public static Date getYearTime(String time) {
        try {
            return TIME_YEAR.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取当前时间月份数字
     */
    public static int getMonth() {
        return Integer.parseInt(TIME_MONTH.format(System.currentTimeMillis()));
    }

    public static int getMonth(long time) {
        return Integer.parseInt(TIME_MONTH.format(time));
    }

    /**
     * 获取当前时间年份数字
     */
    public static int getDate() {
        return Integer.parseInt(TIME_DATE.format(System.currentTimeMillis()));
    }

    public static int getDate(long time) {
        return Integer.parseInt(TIME_DATE.format(time));
    }

    /**
     * 获取当前时间小时数字
     */
    public static int getHour() {
        return Integer.parseInt(TIME_HOUR.format(System.currentTimeMillis()));
    }

    public static int getHour(long time) {
        return Integer.parseInt(TIME_HOUR.format(time));
    }

    /**
     * 获取当前时间分钟数字
     */
    public static int getMinute() {
        return Integer.parseInt(TIME_MINUTE.format(System.currentTimeMillis()));
    }

    public static int getMinute(long time) {
        return Integer.parseInt(TIME_MINUTE.format(time));
    }

    /**
     * 获取当前时间秒数数字
     */
    public static int getSecond() {
        return Integer.parseInt(TIME_SECOND.format(System.currentTimeMillis()));
    }

    public static int getSecond(long time) {
        return Integer.parseInt(TIME_SECOND.format(time));
    }

    /**
     * 获取当前年月字符串(yyyyMM)
     */
    public static String getYm() {
        return TIMEYM.format(System.currentTimeMillis());
    }

    public static String getYm(long time) {
        return TIMEYM.format(time);
    }

    /**
     * 获取当前时分秒
     * 
     * @return
     */
    public static String getHms() {
        return TIME_HMS.format(System.currentTimeMillis());
    }

    /**
     * 获取当前时间字符串(yyyy年MM月dd日)
     */
    public static String getYmdTimeCN(String time) {
        try {
            Date date = TIME_YMD.parse(time);
            return TIME_YMD_CN.format(date.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取当前时间字符串(HH:mm)
     */
    public static String getHmTime() {
        return TIME_HM.format(System.currentTimeMillis());
    }

    public static String getHmTime(long time) {
        return TIME_HM.format(time);
    }

    public static Date getHmTime(String time) {
        try {
            return TIME_HM.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取两个日期之前的日期列表
     */
    public static Set<Date> getDateList(String startDate, String endDate) {
        Date start = null;
        Date end = null;
        try {
            start = TIME_YMD.parse(startDate);
            end = TIME_YMD.parse(endDate);
            Calendar startCalendar = Calendar.getInstance();
            Calendar endCalendar = Calendar.getInstance();
            startCalendar.setTime(start);
            endCalendar.setTime(end);

            Set<Date> result = new TreeSet<Date>();
            if (startCalendar.after(endCalendar)) {
                return result;
            }
            Calendar temp = (Calendar) startCalendar.clone();
            temp.add(Calendar.DAY_OF_YEAR, 1);
            result.add(startCalendar.getTime());
            while (temp.before(endCalendar)) {
                result.add(temp.getTime());
                temp.add(Calendar.DAY_OF_YEAR, 1);
            }
            result.add(endCalendar.getTime());
            return result;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 方法描述:传一个String 类型的时间进来,转换为Date类型的时间
     * 
     * @param strDate
     * @return
     */
    public static Date formatStringToDate(String strDate) {
        try {
            return TIME_YMD.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 计算两个日期之间的差
     * 
     * @param start
     * @param end
     * @param type
     * @return
     */
    public static int diffDateDay(Calendar start, Calendar end, int type) {
        int diff = 0;
        while (start.getTimeInMillis() < end.getTimeInMillis()) {
            diff++;
            start.add(type, 1);
        }
        return diff;
    }

    public static String getUTCTimeStr(boolean isBegin) {
        Calendar c = Calendar.getInstance();
        if (isBegin) {
            c.add(Calendar.DAY_OF_MONTH, -3);
        }
        Date date = c.getTime();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd'T'HH':00:00Z'");
        SimpleDateFormat sdf2 = new SimpleDateFormat("HH':00:00'");
        sdf1.setTimeZone(TimeZone.getTimeZone("UTC"));
        sdf2.setTimeZone(TimeZone.getTimeZone("UTC"));
        String result = sdf1.format(date);
        return result;
    }

    public static String getUTCTimeStr4Minute(boolean isBegin) {
        Calendar c = Calendar.getInstance();
        if (isBegin) {
            c.add(Calendar.MINUTE, -1);
        }
        Date date = c.getTime();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd'T'HH':00:00Z'");
        SimpleDateFormat sdf2 = new SimpleDateFormat("HH':00:00'");
        sdf1.setTimeZone(TimeZone.getTimeZone("UTC"));
        sdf2.setTimeZone(TimeZone.getTimeZone("UTC"));
        String result = sdf1.format(date);
        return result;
    }

    public static Calendar getNextDayCalendar(int days) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, days);
        return c;
    }

    /**
     * 根据年月日返回Calendar对象 (month不需要-1)
     */
    public static Calendar getCalendar(int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.set(year, month - 1, day);
        return c;
    }

    /**
     * 增加Calendar的小时
     */
    public static void addCalendarHour(Calendar c, float hour) {
        c.setTimeInMillis(c.getTimeInMillis() + ((int) (hour * 60 * 60 * 1000)));
    }

    /**
     * 判断一个时间是否在两个日期之内
     */
    public static boolean belongCalendar(long time, String beginTime,
            String endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(getHmTime(getHmTime(time)));

        Calendar begin = Calendar.getInstance();
        begin.setTime(getHmTime(beginTime));

        Calendar end = Calendar.getInstance();
        end.setTime(getHmTime(endTime));

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判两个日期所差分钟
     */
    public static int getMin(String startTime, String endTime) {
        Calendar start = Calendar.getInstance();
        start.setTime(getNormalTime(startTime));
        Calendar end = Calendar.getInstance();
        end.setTime(getNormalTime(endTime));
        long time = end.getTimeInMillis() - start.getTimeInMillis();
        int min = (int) (time / (1000 * 60));
        return min < 0 ? 0 : min;
    }
}
View Code

 

上一篇:向数据库添加日期的转换问题


下一篇:JMeter-BeanShell写数据到CSV文件