如何在今天午夜和明天午夜创建一个Java Date对象?

编程入门 行业动态 更新时间:2024-10-28 15:24:24
本文介绍了如何在今天午夜和明天午夜创建一个Java Date对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的代码中,我需要找到我今天发生的一切事情。所以我需要从今天上午00:00(今天凌晨凌晨)到晚上12点(今晚午夜)的日期进行比较。

In my code I need to find all my things that happened today. So I need to compare against dates from today at 00:00am (midnight early this morning) to 12:00pm (midnight tonight).

我知道...

I know ...

Date today = new Date();

...现在得到我和...

... gets me right now. And ...

Date beginning = new Date(0);

...在1970年1月1日获得零时间。但是什么是简单的方法来获得零时间今天和零时间明天?

... gets me zero time on Jan 1, 1970. But what's an easy way to get zero time today and zero time tomorrow?

任何帮助是非常感谢!

更新;我这样做,但肯定有一个更简单的方法?

UPDATE; I did this, but surely there's an easier way??

Calendar calStart = new GregorianCalendar(); calStart.setTime(new Date()); calStart.set(Calendar.HOUR_OF_DAY, 0); calStart.set(Calendar.MINUTE, 0); calStart.set(Calendar.SECOND, 0); calStart.set(Calendar.MILLISECOND, 0); Date midnightYesterday = calStart.getTime(); Calendar calEnd = new GregorianCalendar(); calEnd.setTime(new Date()); calEnd.set(Calendar.DAY_OF_YEAR, calEnd.get(Calendar.DAY_OF_YEAR)+1); calEnd.set(Calendar.HOUR_OF_DAY, 0); calEnd.set(Calendar.MINUTE, 0); calEnd.set(Calendar.SECOND, 0); calEnd.set(Calendar.MILLISECOND, 0); Date midnightTonight = calEnd.getTime();

推荐答案

java.util.Calendar

java.util.Calendar

// today Calendar date = new GregorianCalendar(); // reset hour, minutes, seconds and millis date.set(Calendar.HOUR_OF_DAY, 0); date.set(Calendar.MINUTE, 0); date.set(Calendar.SECOND, 0); date.set(Calendar.MILLISECOND, 0); // next day date.add(Calendar.DAY_OF_MONTH, 1);

JDK 8 - java.time.LocalTime和java .time.LocalDate

JDK 8 - java.time.LocalTime and java.time.LocalDate

LocalTime midnight = LocalTime.MIDNIGHT; LocalDate today = LocalDate.now(ZoneId.of("Europe/Berlin")); LocalDateTime todayMidnight = LocalDateTime.of(today, midnight); LocalDateTime tomorrowMidnight = todayMidnight.plusDays(1);

Joda-Time

如果您使用JDK< 8,我推荐 Joda时间,因为API真的很棒:

Joda-Time

If you're using a JDK < 8, I recommend Joda Time, because the API is really nice:

DateTime date = new DateTime().toDateMidnight().toDateTime(); DateTime tomorrow = date.plusDays(1);

2.3的Joda时间 DateMidnight is deprecated ,所以使用这个:

Since version 2.3 of Joda Time DateMidnight is deprecated, so use this:

DateTime today = new DateTime().withTimeAtStartOfDay(); DateTime tomorrow = today.plusDays(1).withTimeAtStartOfDay();

如果您不想要JVM的当前默认时区,请传递时区。

Pass a time zone if you don't want the JVM’s current default time zone.

DateTimeZone timeZone = DateTimeZone.forID("America/Montreal"); DateTime today = new DateTime(timeZone).withTimeAtStartOfDay(); // Pass time zone to constructor.

更多推荐

如何在今天午夜和明天午夜创建一个Java Date对象?

本文发布于:2023-06-05 01:25:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/508992.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:午夜   创建一个   对象   明天   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!