时间格式工具类

public class DateFormatUtils {

    public static final long ONE_MINUTE = 60;
    public static final long ONE_HOUR = 3600;
    public static final long ONE_DAY = 86400;
    public static final long ONE_MONTH = 2592000;
    public static final long ONE_YEAR = 31104000;
  

    /**
     * 将当前时间转换为指定格式
     */
    public static String getDate(String format) {
        SimpleDateFormat simple = new SimpleDateFormat(format);
        return simple.format(Calendar.getInstance().getTime());
    }


    /**
     * 将指定的时间转换为指定的格式,若时间为空则默认为当前时间
     */
    public static String getDateAndMinute(Date date) {
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        if (date == null) {
            date = Calendar.getInstance().getTime();
        }
        return simple.format(date);
    }


    /**
     * 距离今天多久
     */
    public static String fromToday(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        long time = date.getTime() / 1000;
        long now = new Date().getTime() / 1000;
        long ago = now - time;
        if (ago <= ONE_HOUR)
            return ago / ONE_MINUTE + "分钟前";
        else if (ago <= ONE_DAY)
            return ago / ONE_HOUR + "小时" + (ago % ONE_HOUR / ONE_MINUTE)
                    + "分钟前";
        else if (ago <= ONE_DAY * 2)
            return "昨天" + calendar.get(Calendar.HOUR_OF_DAY) + "点"
                    + calendar.get(Calendar.MINUTE) + "分";
        else if (ago <= ONE_DAY * 3)
            return "前天" + calendar.get(Calendar.HOUR_OF_DAY) + "点"
                    + calendar.get(Calendar.MINUTE) + "分";
        else if (ago <= ONE_MONTH) {
            long day = ago / ONE_DAY;
            return day + "天前" + calendar.get(Calendar.HOUR_OF_DAY) + "点"
                    + calendar.get(Calendar.MINUTE) + "分";
        } else if (ago <= ONE_YEAR) {
            long month = ago / ONE_MONTH;
            long day = ago % ONE_MONTH / ONE_DAY;
            return month + "个月" + day + "天前"
                    + calendar.get(Calendar.HOUR_OF_DAY) + "点"
                    + calendar.get(Calendar.MINUTE) + "分";
        } else {
            long year = ago / ONE_YEAR;
            int month = calendar.get(Calendar.MONTH) + 1;// JANUARY which is 0 so month+1
            return year + "年前" + month + "月" + calendar.get(Calendar.DATE)
                    + "日";
        }

    }



    /**
     * 距离截止日期还有多长时间
     */
    public static String fromDeadline(Date date) {
        long deadline = date.getTime() / 1000;
        long now = (new Date().getTime()) / 1000;
        long remain = deadline - now;
        if (remain <= ONE_HOUR)
            return "只剩下" + remain / ONE_MINUTE + "分钟";
        else if (remain <= ONE_DAY)
            return "只剩下" + remain / ONE_HOUR + "小时"
                    + (remain % ONE_HOUR / ONE_MINUTE) + "分钟";
        else {
            long day = remain / ONE_DAY;
            long hour = remain % ONE_DAY / ONE_HOUR;
            long minute = remain % ONE_DAY % ONE_HOUR / ONE_MINUTE;
            return "只剩下" + day + "天" + hour + "小时" + minute + "分钟";
        }

    }


    //获取年
    public static int getYearInt() {
        return Calendar.getInstance().get(Calendar.YEAR);
    }

    //获取月
    public static int getMonthInt() {
        return Calendar.getInstance().get(Calendar.MONTH);
    }

    //获取日
    public static int getDayInt() {
        return Calendar.getInstance().get(Calendar.DATE);
    }

    //获取小时
    public static int get24Hour() {
        return Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
    }

    //获取分钟
    public static int getMinute() {
        return Calendar.getInstance().get(Calendar.MINUTE);
    }

    //获取秒
    public static int getSecond() {
        return Calendar.getInstance().get(Calendar.SECOND);
    }


    /**
     * 获取增加小时和分钟后的时间
     */
    public static int[] getNowTimeAdd(int newHour, int newMinute) {
        int[] time = new int[2];
        time[0] = get24Hour() + newHour + (getMinute() + newMinute) / 60;
        time[1] = (getMinute() + newMinute) % 60;
        return time;
    }


    /**
     * 保留 两位小数
     * 0 占位符,没有则填补为0
     * # 没有则不显示
     */
    public static String getLangweiStr(String s) {
        if (s == null || s.isEmpty()) {
            return "0.00";
        }
        DecimalFormat df = new DecimalFormat("#0.00");
        Double d = Double.parseDouble(s);
        return (df.format(d));
    }



    /**
     * 保留 两位小数
     */
    public static String getLangweiStr(Double d) {
        DecimalFormat df = new DecimalFormat("#0.00");
        return (df.format(d));
    }




    /**
     * 获取当前的小时:分钟
     */
    public static String getCurHourAndMin() {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        return format.format(Calendar.getInstance().getTime());
    }



    /**
     * 将指定时间转换为指定格式
     */
    public static String getTime(long time, String timeFormat) {
        SimpleDateFormat format = new SimpleDateFormat(timeFormat);
        return format.format(new Date(time));
    }

    

    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s) {
        return stampToDate(s, true);
    }


    /**
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s, boolean ismills) {
        String format = "yyyy-MM-dd HH:mm";
        if (ismills)
            format += ":ss";
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

 

    /**
     * 将时间戳转换为时间
     */
    public static String stampToDate(long stamp) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(stamp);
        return simpleDateFormat.format(date);
    }



    /**
     * 将时间戳转换为年月日
     */
    public static String stampToDateYMD(long time) {
        String format = "yyyy-MM-dd";
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date = new Date(time);
        long ss = date.getTime();
        res = simpleDateFormat.format(date);
        return res;
    }

  


    /**
     * 计算时间差
     *
     * @param startTime
     * @param endTime
     * @return [0]天 [1] 小时数 [2] 分钟数 [3] 秒数
     */
    public static long[] getTimeExpend(long startTime, long endTime) {
        long[] timediff = {0, 0, 0, 0};
        long longExpend = endTime - startTime;  //获取时间差
        timediff[0] = longExpend / (24 * 60 * 60 * 1000); //根据时间差来计算天数
        timediff[1] = (longExpend - timediff[0] * (24 * 60 * 60 * 1000)) / (60 * 60 * 1000); //根据时间差来计算小时数
        timediff[2] = (longExpend - timediff[0] * (24 * 60 * 60 * 1000) - timediff[1] * (60 * 60 * 1000)) / (60 * 1000);   //根据时间差来计算分钟数
        timediff[3] = (longExpend - timediff[0] * (24 * 60 * 60 * 1000) - timediff[1] * (60 * 60 * 1000) - timediff[2] * (60 * 1000)) / 1000;   //根据时间差来计算秒数
        return timediff;
    }




    /**
     * 转换时间日期格式字符串为long型
     */
    public static Long convertTimeToLong(String time, SimpleDateFormat sdf) {
        Date date = null;
        try {
            date = sdf.parse(time);
            return date.getTime();
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        }
    }


    /**
     * 获取指定月份的天数
     *
     * @param year
     * @param month
     * @return
     */
    public static int getDaysByYearMonth(int year, int month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }



    /**
     * 获取指定年月对象
     *
     * @param year
     * @param month
     * @return
     */
    public static Calendar getCalendarByYearMonth(int year, int month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        return a;
    }



    /**
     * 指定今天的时间对象
     *
     * @param hours
     * @param minute
     * @return
     */
    public static Calendar getCalendarByHourMinute(int hours, int minute) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.HOUR_OF_DAY, hours);
        a.set(Calendar.MINUTE, minute);
        a.set(Calendar.SECOND, 0);
        a.set(Calendar.MILLISECOND, 0);
        return a;
    }



    /**
     * 获取累加天数后的日期
     *
     * @param date
     * @param day
     * @return
     */
    public static Date getDateBy(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, day);
        return calendar.getTime();
    }

   /**
     * 获取下周, 周一和周天的日期
     * @param time
     * @param format
     * @return
     */
    public static Map<String, String> getNextWeek(long time, String format) {
        Map<String, String> dateMap = new HashMap<String, String>();
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(new Date(time));
        cal2.setTime(new Date(time));
        int dayWeek = cal1.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (dayWeek == 1) {
            cal1.add(Calendar.DAY_OF_MONTH, 1);
            cal2.add(Calendar.DAY_OF_MONTH, 7);
        } else {
            cal1.add(Calendar.DAY_OF_MONTH, 1 - dayWeek + 8);
            cal2.add(Calendar.DAY_OF_MONTH, 1 - dayWeek + 14);
        }
        String nextMonday = sdf.format(cal1.getTime());
        String nextSunday = sdf.format(cal2.getTime());
        dateMap.put("nextMonday", nextMonday);
        dateMap.put("nextSunday", nextSunday);
        return dateMap;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值