gpt4 book ai didi

java - Java 任务需要两个 Date() 值

转载 作者:行者123 更新时间:2023-12-01 07:41:48 26 4
gpt4 key购买 nike

这是一个非常简单的请求,但我不太确定生成这两个值的最简单/最有效的方法。

我需要编写一个脚本来检查给定值是否在两个值之间。我很清楚这是如何在 SQL 中完成的。

我需要这些值的方式类似于以下内容。

Date testValue = new Date()       //This represents the value we are testing

Date beginningOfDay = .... //This value would represent the date for
testValue at 12:00am

Date endOfDay = ... //This value would represent the date for
testValue at 11:59:59pm

同样,Java Date() 类型可能不是执行此类操作的最佳实践。最后我只需要生成三个我可以说的值

if testValue is after beginningOfDay && testValue is before endOfDay 
//do logic

最佳答案

我个人为此使用日历对象。例如:

Date testDate = ??? //Replace with whatever source you are using
Calendar testDateCalendar = Calendar.getInstance();
testDateCalendar.setTime(testDate);

Date today = new Date();
Calendar endOfDay = Calendar.getInstance(); //Initiates to current time
endOfDay.setTime(today);
endOfDay.set(Calendar.HOUR_OF_DAY, 23);
endOfDay.set(Calendar.MINUTE, 59);
endOfDay.set(Calendar.SECOND, 59);

Calendar startOfDay = Calendar.getInstance();
startOfDay.setTime(today);
startOfDay.set(Calendar.HOUR_OF_DAY, 0);
startOfDay.set(Calendar.MINUTE, 0);
startOfDay.set(Calendar.SECOND, 0);

if (startOfDay.before(testDateCalendar) && endOfDay.after(testDateCalendar))
{
//Whatever
} else {
//Failure
}

关于java - Java 任务需要两个 Date() 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3962311/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com