如何从Date删除毫秒,秒,分和小时 [英] How to remove milliseconds, seconds, minutes and hours from Date

查看:504
本文介绍了如何从Date删除毫秒,秒,分和小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,我想比较两个日期。不过,我只想比较年,月,日。这是我可以做的:

I ran into a problem where I wanted to compare two dates. However, I only wanted to compare Year, Month and Day. And this is what I can up with:

 private Date trim(Date date) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR, 0);
        return calendar.getTime();
    }

我用这个功能修剪所有的单位,但是几天,几个月和几年。

I user this function to trim all units but days, months and years.

现在,问题是你怎么看?
你知道其他任何方式吗?

Now, the question is what do you think about it? Do you know any other way to do so?

谢谢

推荐答案

你真的需要使用 java.util.Date ?如果您可以切换到 joda时间,您会发现非常好的功能,如:

Do you really need to use java.util.Date ? If you can switch to joda time, you'll find very nice features like:

dateTime.dayOfMonth().roundFloorCopy()

它完全符合您的需要。

这篇关于如何从Date删除毫秒,秒,分和小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆