Java中常量的最后一小时,分钟和秒

时间:2022-08-09 02:25:23

Are there convenient constants in Android or Java for the last hour, minute or second of the day?

在一天的最后一小时,一分钟或二分之一的Android或Java中是否有方便的常量?

I found myself declaring the constants

我发现自己宣称了这些常数

private static final int LAST_HOUR_OF_DAY = 23;
private static final int LAST_MINUTE = 59;
private static final int LAST_SECOND = 59;

and wondered if I could already get these somewhere.

并想知道我是否已经可以在某处获得这些。

2 个解决方案

#1


You can use Joda-Time for this:

你可以使用Joda-Time:

Date date = //the date you want to work wit
Date endOfDay = new DateTime(date).withTimeAtStartOfDay().plusDays(1).minusSeconds(1).toDate();

However note that there is a gap of 1s between the resulting Date and the actual end of the day. I would suggest working with the start of the next day and using before.

但请注意,结果日期与当天实际结束之间存在1秒的差距。我建议在第二天开始工作并使用之前。

#2


Joda-Time has what you are looking for

Joda-Time有你想要的东西

http://www.joda.org/joda-time/

#1


You can use Joda-Time for this:

你可以使用Joda-Time:

Date date = //the date you want to work wit
Date endOfDay = new DateTime(date).withTimeAtStartOfDay().plusDays(1).minusSeconds(1).toDate();

However note that there is a gap of 1s between the resulting Date and the actual end of the day. I would suggest working with the start of the next day and using before.

但请注意,结果日期与当天实际结束之间存在1秒的差距。我建议在第二天开始工作并使用之前。

#2


Joda-Time has what you are looking for

Joda-Time有你想要的东西

http://www.joda.org/joda-time/