时间工具类TimeUtils

本文介绍了一个Java时间处理工具类,提供了获取当前时间戳、将时间戳转换为日期格式等多种实用功能,并展示了如何使用这些方法进行时间操作。
public class TimeUtils {

    /**
     * 返回当前时间戳
     *
     * @return 当前时间戳, 单位 毫秒
     */
    public static long now() {
        return System.currentTimeMillis();
    }

    /**
     * long类型的时间戳转换成日期的格式来显示
     *
     * @param timeStamp 单位毫秒
     * @return 日期
     */
    public static String parseTimeStamps(long timeStamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        return sdf.format(new Date(timeStamp));
    }


    /**
     * long类型的时间戳转换成日期的格式来显示
     *
     * @param timeStamp 单位毫秒
     * @return 日期
     */
    public static String parseTimeStamp(long timeStamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        return sdf.format(new Date(timeStamp));
    }

    /**
     * long类型的时间戳转换成日期的格式来显示
     *
     * @param date 日期
     * @return 日期
     */
    public static String parseTimeStamp(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        return sdf.format(date);
    }

    /**
     * long类型的时间戳转换成日期的格式来显示:"yyyy-MM-dd 星期"
     *
     * @param timeStamp 单位毫秒
     * @return 日期
     */
    public static String parseTimeStamp2(long timeStamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd EEEE", Locale.CHINA);
        return sdf.format(new Date(timeStamp));
    }

    /**
     * long类型的时间戳转换成日期的格式来显示:"HH:mm:ss"
     *
     * @param timeStamp 单位毫秒
     * @return 日期
     */
    public static String parseTimeStamp3(long timeStamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.CHINA);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT +0800"));
        return sdf.format(new Date(timeStamp));
    }


    /**
     * 显示当前时间
     *
     * @param format 显示格式
     * @return 当前日期
     */
    public static String getCurrentTime(String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
        return sdf.format(new Date(now()));
    }

    /**
     * 获取当前时间戳,精确到天
     *
     * @return 时间戳
     */
    public static long getCurDateTimeStamp() {
        return System.currentTimeMillis() / 86400000 * 86400000;
    }

    /**
     * 获取当前时间戳,精确到分钟
     */
    public static long getCurMinuteTimeStamp() {
        return System.currentTimeMillis() / 60000 * 60000;
    }

    /**
     * 获取当前是星期几
     *
     * @return 0[星期天] 1[星期一] 2[星期二] 3[星期三] 4[星期四] 5[星期五] 6[星期六]
     */
    public static int getCurWeekday() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(System.currentTimeMillis()));
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        return w < 0 ? 0 : w;
    }

    /**
     * 将【秒数】转为【天-小时--秒】返回
     */
    public static String formatTime(long sec) {
        String time;
        long days = sec / (60 * 60 * 24);
        long hours = (sec % (60 * 60 * 24)) / (60 * 60);
        long minutes = (sec % (60 * 60)) / 60;
        long seconds = sec % 60;
        if (days > 0) {
            time = days + "" + hours + "小时" + minutes + "分钟" + seconds + "";
        } else if (hours > 0) {
            time = hours + "小时" + minutes + "分钟" + seconds + "";
        } else if (minutes > 0) {
            time = minutes + "分钟" + seconds + "";
        } else {
            time = seconds + "";
        }

        return time;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值