从 java.util.Date 中删除时间部分的正确方法是什么?

发布于 2024-12-09 20:49:11 字数 634 浏览 0 评论 0原文

我想实现一个线程安全函数来从 java.util.Date 中删除时间部分。

我尝试了这种方式

private static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
public static Date removeTimeFromDate(Date date) {
        Date returnDate = date;

        if (date == null) {
            return returnDate;
        }

        //just have the date remove the time
        String targetDateStr = df.format(date);
        try {
            returnDate = df.parse(targetDateStr);
        } catch (ParseException e) {
        }

        return returnDate;
}

并使用synchronized或threadLocal使其线程安全。 但有没有更好的方法在 Java 中实现它。看来这种方式有点啰嗦。 我对此并不满意。

I want to implement a thread-safe function to remove the time part from java.util.Date.

I tried this way

private static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
public static Date removeTimeFromDate(Date date) {
        Date returnDate = date;

        if (date == null) {
            return returnDate;
        }

        //just have the date remove the time
        String targetDateStr = df.format(date);
        try {
            returnDate = df.parse(targetDateStr);
        } catch (ParseException e) {
        }

        return returnDate;
}

and use synchronized or threadLocal to make it thread-safe.
But it there any better way to implement it in Java. It seems this way is a bit verbose.
I am not satisfied with it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

命硬 2024-12-16 20:49:11

Date 对象保存一个变量,该变量表示自 epoch< 以来的毫秒数/a>.因此,您无法“删除”时间部分。您可以做的是将当天的时间设置为零,这意味着它将是当天的 00:00:00 000。这是通过使用 GregorianCalendar 来完成的:

GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
gc.set(Calendar.HOUR_OF_DAY, 0);
gc.set(Calendar.MINUTE, 0);
gc.set(Calendar.SECOND, 0);
gc.set(Calendar.MILLISECOND, 0);
Date returnDate = gc.getTime();

A Date object holds a variable wich represents the time as the number of milliseconds since epoch. So, you can't "remove" the time part. What you can do is set the time of that day to zero, which means it will be 00:00:00 000 of that day. This is done by using a GregorianCalendar:

GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
gc.set(Calendar.HOUR_OF_DAY, 0);
gc.set(Calendar.MINUTE, 0);
gc.set(Calendar.SECOND, 0);
gc.set(Calendar.MILLISECOND, 0);
Date returnDate = gc.getTime();
紫瑟鸿黎 2024-12-16 20:49:11

Date 保存一个即时时间 - 这意味着它没有明确指定特定日期。因此,您还需要指定时区,以便计算出某件事发生的日期。然后,您需要弄清楚如何表示结果 - 例如,将结果表示为值为“UTC 日期的午夜”的日期?

您还应该注意,由于 DST 转换可能发生在午夜,因此并非所有时区的所有日子都会出现午夜。 (巴西就是一个常见的例子。)

除非您真的非常喜欢日期日历,否则我建议您开始使用相反,Joda Time,因为它允许您拥有 LocalDate 类型的值,该值摆脱大部分这些问题。

A Date holds an instant in time - that means it doesn't unambiguously specify a particular date. So you need to specify a time zone as well, in order to work out what date something falls on. You then need to work out how you want to represent the result - as a Date with a value of "midnight on that date in UTC" for example?

You should also note that midnight itself doesn't occur on all days in all time zones, due to DST transitions which can occur at midnight. (Brazil is a common example of this.)

Unless you're really wedded to Date and Calendar, I'd recommend that you start using Joda Time instead, as that allows you to have a value of type LocalDate which gets rid of most of these problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文