java中 快速获取当天0点0分0秒(00:00:00),23点59分59秒(23:59:59)

本文详细介绍了如何使用Java中的Calendar和DateFormatUtils来获取当前日期的凌晨0点0分0秒及23点59分59秒的时间戳和字符串表示。通过实例演示了如何设置Calendar对象来精确到指定时间点,并利用Apachelang包快速格式化日期。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 利用Apache lang包快速获取凌晨0点0分0秒,23点59分59秒字符串
        System.out.println(DateFormatUtils.format(new Date(), "yyyy-MM-dd 00:00:00"));
        System.out.println(DateFormatUtils.format(new Date(), "yyyy-MM-dd 23:59:59"));


// 获取当天凌晨0点0分0秒Date
        Calendar calendar1 = Calendar.getInstance();
        calendar1.set(calendar1.get(Calendar.YEAR), calendar1.get(Calendar.MONTH), calendar1.get(Calendar.DAY_OF_MONTH),
                0, 0, 0);
        Date beginOfDate = calendar1.getTime();
        System.out.println(beginOfDate);

// 获取当天23点59分59秒Date
        Calendar calendar2 = Calendar.getInstance();
        calendar1.set(calendar2.get(Calendar.YEAR), calendar2.get(Calendar.MONTH), calendar2.get(Calendar.DAY_OF_MONTH),
                23, 59, 59);
        Date endOfDate = calendar1.getTime();
        System.out.println(endOfDate);

--------------------- 
作者:不羁鱼Karle 
来源:优快云 
原文:https://blog.youkuaiyun.com/qq_19260029/article/details/51489928 
版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值