时间处理的工具类

package com.huaiye.ecs.common.utils;


import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class TimeUtil {
    private final static String TIME_FORMAT = "HH:mm";
    private final static String TIME_FORMAT_YMDHMS = "yyyy-MM-dd HH:mm:ss";
    private final static String TIME_FORMAT_YMDHM = "yyyy-MM-dd HH:mm";
    private final static String TIME_FORMAT_MDHMS = "MM-dd HH:mm:ss";
    private final static String TIME_FORMAT_YMD = "yyyy-MM-dd";

    private final static String TIME_FORMAT_YMD_2 = "yyyyMMdd";

    /**
     * 将时间显示为HH:mm:SS,注意HH不是24小时制也不是12小时制,是累计的小时数,
     * 可能是大于24,此方法主要用于播放器界面的时间显示
     *
     * @param millisecond 时间毫秒
     * @return HH:mm:SS
     */
    public static String formatForPlayer(long millisecond) {
        DecimalFormat df = new DecimalFormat("00");
        StringBuilder timeString = new StringBuilder();
        long hour = millisecond / (1000 * 60 * 60);
        long minute = millisecond % (1000 * 60 * 60) / (1000 * 60);
        long second = millisecond % (1000 * 60) / 1000;
        timeString.append(df.format(hour)).append(":");
        timeString.append(df.format(minute)).append(":")
                .append(df.format(second));
        return timeString.toString();
    }

    /**
     * 获取当前时间的小时和分钟
     *
     * @return HH:mm
     */
    public static String getCurFormatTime() {
        SimpleDateFormat df = new SimpleDateFormat(TIME_FORMAT, Locale.CHINA);
        return df.format(new Date());
    }

    /**
     * 获取当前时间的yyyy-MM-dd HH:mm:ss格式
     *
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getCurYMDTime() {
        SimpleDateFormat df = new SimpleDateFormat(TIME_FORMAT_YMDHMS, Locale.CHINA);
        return df.format(new Date());
    }

    public static String getCurYMDFTime() {
        SimpleDateFormat df = new SimpleDateFormat(TIME_FORMAT_YMDHM, Locale.CHINA);
        return df.format(new Date());
    }

    //获取上一天的时间
    public static String getYesTody(Date date) {
        SimpleDateFormat df = new SimpleDateFormat(TIME_FORMAT_YMDHMS, Locale.CHINA);
        return df.format(date);
    }

    /**
     * yyyy-MM-dd HH:mm:ss格式的时间转变为MM-dd HH:mm:ss
     *
     * @param dateString 时间字符串
     * @return 转变后的值
     */
    public static String formatToMDHms(String dateString) {
        String result = "";
        Date date = getDateFromString(dateString);
        if (date != null) {
            result = getDateStringMdHms(date);
        }
        return result;
    }

    /**
     * yyyy-MM-dd HH:mm:ss格式的时间转变为Date类型
     *
     * @param dateString 时间字符串
     * @return 转变后的值
     */
    public static Date getDateFromString(String dateString) {
        SimpleDateFormat df = new SimpleDateFormat(TIME_FORMAT_YMDHMS, Locale.CHINA);
        Date date = null;
        try {
            date = df.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * "yyyy-MM-dd HH:mm:ss"格式的日期转为long类型的time
     *
     * @param timeString 时间字符串
     * @return long类型的time
     */
    public static long getCommonTime(String timeString) {
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT_YMDHMS, Locale.CHINA);
        try {
            Date d = sdf.parse(timeString);
            return d.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * "yyyy-MM-dd HH:mm:ss"格式的日期转为"yyyy-MM-dd"格式
     *
     * @param timeString 时间字符串
     * @return long类型的time
     */
    public static String formatDateStr(String timeString) {
        SimpleDateFormat sdfBefore = new SimpleDateFormat(TIME_FORMAT_YMDHMS, Locale.CHINA);
        SimpleDateFormat sdfAfter = new SimpleDateFormat(TIME_FORMAT_YMD, Locale.CHINA);
        try {
            return sdfAfter.format(sdfBefore.parse(timeString));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }


    /**
     * "yyyy-MM-dd格式的日期转为long类型的time
     *
     * @param timeString 时间字符串
     * @return long类型的time
     */
    public static long getYMDCommonTime(String timeString) {
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT_YMD, Locale.CHINA);
        try {
            Date d = sdf.parse(timeString);
            return d.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static long getHoursLong() {
        String curFormatTime = getCurFormatTime();
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT, Locale.CHINA);
        try {
            Date d = sdf.parse(curFormatTime);
            return d.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 将日期转换为yyyy-MM-dd HH:mm:ss格式字符串
     *
     * @param date 日期
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getDateString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT_YMDHMS, Locale.CHINA);
        return sdf.format(date);
    }

    /**
     * 将日期转换为MM-dd HH:mm:ss格式字符串
     *
     * @param date 日期
     * @return MM-dd HH:mm:ss
     */
    public static String getDateStringMdHms(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT_MDHMS, Locale.CHINA);
        return sdf.format(date);
    }

    /**
     * 将日期转换为yyyy-MM-dd格式字符串
     *
     * @param date 日期
     * @return yyyy-MM-dd
     */
    public static String getYMDString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT_YMD, Locale.CHINA);
        return sdf.format(date);
    }

    /**
     * 将日期转换为yyyy-MM-dd格式字符串
     *
     * @param date 日期
     * @return yyyyMMdd
     */
    public static String getYMDString2(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT_YMD_2, Locale.CHINA);
        return sdf.format(date);
    }

    /**
     * 得到某年某月的第一天
     */
    public static String getFirstDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
        return new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(cal.getTime());
    }

    /**
     * 得到某年某月的最后一天
     */
    public static String getLastDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, value);
        return new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(cal.getTime());
    }


    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    public static String getDate(long time) {
        String times = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        times = sdf.format(time);
        return times;
    }

    public static String getTimeVideo(long time) {
        String times = "";
        SimpleDateFormat sdf = new SimpleDateFormat("dd HH:mm:ss");
        times = sdf.format(time);
        return times;
    }

    public static String getDateDay(long time) {
        String times = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        times = sdf.format(time);
        return times;
    }

    public static Calendar StringToCalendar(String time) {
        Date date = null;
        Calendar calendar = null;
        try {
            date = sdf.parse(time);
            calendar = Calendar.getInstance();
            calendar.setTime(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return calendar;

    }

    public static Calendar LongToCalendar(long time) {
        Date date = null;
        Calendar calendar = null;
        try {
            date = new Date(time);
            calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(calendar.MONTH, -3);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return calendar;

    }

    public static int getDistanceMonth(String date1, String date2) {
        int distance = 0;
        int distanceTime = 0;
        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        try {
            calendar1.setTime(sdf.parse(date1));
            calendar2.setTime(sdf.parse(date2));
            distance = calendar2.get(Calendar.MONTH) - calendar1.get(Calendar.MONTH);
            distanceTime = calendar2.get(Calendar.DAY_OF_MONTH) - calendar1.get(Calendar.DAY_OF_MONTH);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (distance == 3 && distanceTime > 0)
            distance = 4;
        return distance;
    }

    /**
     * 获取两个时间点之间的时间间隔
     *
     * @param date1 第一个时间点
     * @param date2 第二个时间点
     * @return 时间间隔
     */
    public static String getDistanceTime(String date1, String date2) {
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT_YMDHMS);
        try {
            long time1 = sdf.parse(date1).getTime();
            long time2 = sdf.parse(date2).getTime();

            int second = (int) ((time2 - time1) / 1000);
            return getVideoLength(second);
        } catch (ParseException e) {
            e.printStackTrace();
            return "0";
        }
    }

    /**
     * 获取两个时间点之间的时间间隔
     *
     * @param date1 第一个时间点
     * @param date2 第二个时间点
     * @return 时间间隔
     */
    public static String getDistanceTextTime(String date1, String date2) {
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT_YMDHMS);
        try {
            long time1 = sdf.parse(date1).getTime();
            long time2 = sdf.parse(date2).getTime();

            int second = (int) ((time2 - time1) / 1000);
            return getVideoTextLength(second);
        } catch (ParseException e) {
            e.printStackTrace();
            return "0";
        }
    }

    /**
     * 获取格式化之后的视频时间长度
     *
     * @return 视频时间
     */
    public static String getVideoTextLength(int videoSecond) {

        int hour = 0;
        int minute = 0;
        int second = 0;

        if (videoSecond > 0) {
            hour = videoSecond / (60 * 60);
            minute = (videoSecond - hour * 60 * 60) / 60;
            second = videoSecond % 60;
        }
        int time_hour = Integer.parseInt(formatTimeUnite(hour));
        int time_minute = Integer.parseInt(formatTimeUnite(minute));
        int time_second = Integer.parseInt(formatTimeUnite(second));
        if (time_hour > 0) {
            return time_hour + "小时" + time_minute + "分钟" + time_second + "";
        }
        if (time_minute > 0) {
            return time_minute + "分钟" + time_second + "";
        }
        if (time_second > 0) {
            return time_second + "";
        }
        return "0";
    }

    public static String getDistanceTime(long date2) {
        int timeLong = (int) (date2 * 1000);
        int hour = new Long(timeLong / (1000 * 60 * 60)).intValue();
        int tempLeft_minute = new Long(timeLong % (1000 * 60 * 60))
                .intValue();
        int minute = new Long(tempLeft_minute / (1000 * 60)).intValue();
        int tempLeft_second = new Long(tempLeft_minute % (1000 * 60))
                .intValue();
        int second = new Long(tempLeft_second / 1000).intValue();

        StringBuilder sb = new StringBuilder();
        if (hour > 0) {
            if (hour > 9)
                sb.append(hour + ":");
            else
                sb.append("0" + hour + ":");
        } else {
            sb.append("00:");
        }

        if (minute > 0) {
            if (minute > 9)
                sb.append(minute + ":");
            else
                sb.append("0" + minute + ":");
        } else {
            sb.append("00:");
        }

        if (second > 0) {
            if (second > 9)
                sb.append(second + "");
            else
                sb.append("0" + second + "");
        } else {
            sb.append("00:");
        }

        return sb.toString();
    }

    public static Calendar getCurrentCalendar() {
        Calendar calendar = Calendar.getInstance();
        return calendar;
    }


    /**
     * 获取格式化之后的视频时间长度
     *
     * @return 视频时间
     */
    public static String getVideoLength(int videoSecond) {

        int hour = 0;
        int minute = 0;
        int second = 0;

        if (videoSecond > 0) {
            hour = videoSecond / (60 * 60);
            minute = (videoSecond - hour * 60 * 60) / 60;
            second = videoSecond % 60;
        }

        return formatTimeUnite(hour) + ":" + formatTimeUnite(minute) + ":" + formatTimeUnite(second);
    }

    /**
     * 格式化时间组成单元
     *
     * @param num 时间
     * @return 格式化后的时间
     */
    private static String formatTimeUnite(int num) {
        if (num > 9) {
            return String.valueOf(num);
        } else {
            return "0" + num;
        }
    }

    /**
     * 转换成计时时间 000000
     *
     * @param ttime
     * @return
     */
    public static String timeCalculate(long ttime) {
        long daysuuu, hoursuuu, minutesuuu, secondsuuu;
        String daysT = "", restT = "";
        daysuuu = (Math.round(ttime) / 86400);
        hoursuuu = (Math.round(ttime) / 3600) - (daysuuu * 24);
        minutesuuu = (Math.round(ttime) / 60) - (daysuuu * 1440) - (hoursuuu * 60);
        secondsuuu = Math.round(ttime) % 60;
        if (daysuuu == 1) daysT = String.format("%d day ", daysuuu);
        if (daysuuu > 1) daysT = String.format("%d days ", daysuuu);
        restT = String.format("%02d:%02d:%02d", hoursuuu, minutesuuu, secondsuuu);
        return daysT + restT;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值