Java时间处理:根据日期计算并转化为前天昨天凌晨早上下午晚上明天后天15天内等标签

根据提供的Date计算并转换为常用的日期标签:前天、昨天、凌晨、早上、下午、晚上、明天、后天、15天内等。

    public static String getAutoTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        Calendar currentCalendar = Calendar.getInstance();
        currentCalendar.set(Calendar.HOUR_OF_DAY, 0);
        currentCalendar.set(Calendar.MINUTE, 0);
        currentCalendar.set(Calendar.SECOND, 0);
        currentCalendar.set(Calendar.MILLISECOND, 0);
        long dateL = calendar.getTimeInMillis();
        long nowL = currentCalendar.getTimeInMillis();
        if (dateL < nowL - 1296000000L) { // 比15天前还早
            if (calendar.get(Calendar.YEAR) == currentCalendar.get(Calendar.YEAR)) { // 在今年
                return getDate(date);
            } else {
                return getDateOneYear(date);
            }
        } else if (dateL < nowL - 172800000L) { // 比今天0时还早48小时以上
            return ((nowL - dateL) / 86400000L) + "天前";
        } else if (dateL < nowL - 86400000L) { // 比今天0时还早24小时以上
            return "前天 " + getTime(date);
        } else if (dateL < nowL) { // 比今天0时还早
            return "昨天 " + getTime(date);
        } else if (dateL < nowL + 21600000L) { // 今天6点前
            return "凌晨 " + getTime(date);
        } else if (dateL < nowL + 43200000L) { // 今天12点前
            return "早上 " + getTime(date);
        } else if (dateL < nowL + 64800000L) { // 今天18点前
            return "下午 " + getTime(date);
        } else if (dateL < nowL + 86400000L) { // 明天0时前
            return "晚上 " + getTime(date);
        } else if (dateL < nowL + 172800000L) { // 晚今天0时48小时内
            return "明天 " + getTime(date);
        } else if (dateL < nowL + 259200000L) { // 晚今天0时72小时内
            return "后天 " + getTime(date);
        } else {
            return getDateOneYear(date);
        }
    }

格式化日期的方法


    public static String getBaseDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:00", Locale.getDefault());
        return sdf.format(date);
    }
    public static String getDateOneYear(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日", Locale.getDefault());
        return sdf.format(date);
    }

    public static String getDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日", Locale.getDefault());
        return sdf.format(date);
    }

    public static String getTime(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.getDefault());
        return sdf.format(date);
    }

    public static String getWeek(int value) {
        switch (value) {
            case 1:
                return "周日";
            case 2:
                return "周一";
            case 3:
                return "周二";
            case 4:
                return "周三";
            case 5:
                return "周四";
            case 6:
                return "周五";
            default:
                return "周六";
        }
    }

获取今天昨天前天本周上周上上周时间段:https://blog.csdn.net/Honiler/article/details/89173722

  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Homilier

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值