我正在尝试创建一个仅在给定时间内工作的函数。
也就是说:如果startTime是"13:00“,endTime是"14:00”,那么当用户每天在这段时间使用我的应用程序时,调用函数,否则什么也不做。
这是我在试图弄清楚的时候遇到的,但这不起作用。
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.UK);
try {
Date inTime = sdf.parse("13:00");
Date outTime = sdf.parse("14:00");
if (outTime != null) {
if (isTimeAfter(inTime, outTime)) {
Toast.makeText(SplashScreenActivity.this, "Time validation success", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(SplashScreenActivity.this, "Exit time must be greater then entry time", Toast.LENGTH_LONG).show();
}
}
} catch (ParseException e) {
e.printStackTrace();
Toast.makeText(SplashScreenActivity.this, "Parse error", Toast.LENGTH_LONG).show();
}
public static boolean isTimeAfter(Date startTime, Date endTime) {
return !endTime.before(startTime);
}
如有任何帮助,我们将不胜感激,谢谢!
更新1
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.UK);
Date now = new Date(System.currentTimeMillis());
try {
Date inTime = sdf.parse("03:19");
Date outTime = sdf.parse("03:21");
if (now.after(inTime) && now.before(outTime)) {
Toast.makeText(SplashScreenActivity.this, "Time validation success", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(SplashScreenActivity.this, "Nope, it isn't wokring", Toast.LENGTH_LONG).show();
}
} catch (ParseException e) {
e.printStackTrace();
Toast.makeText(SplashScreenActivity.this, "Parse error", Toast.LENGTH_LONG).show();
}
发布于 2021-02-25 06:08:08
LocalTime start = null ;
LocalTime end = null ;
try
{
start = LocalTime.parse( "03:19" ) ;
end = LocalTime.parse( "03:21" ) ;
}
catch ( DateTimeParseException e )
{
… handle faulty input
}
LocalTime currentTimeOfDay = LocalTime.now( ZoneId.systemDefault() ) ;
if(
( ! currentTimeOfDay.isBefore( start ) ) // "Is not before …" is a briefer way of asking "Is equal to or later than …".
&&
currentTimeOfDay.isBefore( end )
)
{ … within target times }
else
{ … outside target times }
Date
&SimpleDateFormat
The Yoni 的回答接近于正确的解决方案,但通过使用java.util.Date
类。从不使用java.util.Date
,停下来,让它过去,继续前进。
那些遗留的日期时间类是一团糟。 Sun、Oracle 和 JCP 社区在几年前随着 JSR 310 的采用而放弃了它们。你也应该如此。
你说过:
也就是说:如果startTime是"13:00“,endTime是"14:00”,那么当用户每天在这段时间使用我的应用程序时,调用函数,否则什么也不做。
LocalTime
表示您的开始和结束时间。
LocalTime start = LocalTime.of( 13 , 0 ) ;
LocalTime end = LocalTime.of( 14 , 0 ) ;
捕捉在特定时区中看到的当前时刻。
ZoneId
您可以假设用户希望使用JVM的默认时区。
ZoneId zoneId = ZoneId.systemDefault() ;
如果情况危急,您应与用户确认他们的预期时区。指定实时时区
使用以下格式的名称Continent/Region
,而不是2-4个字母的伪区,例如IST
, EST
, PST
,等等。
ZoneId zoneId = ZoneId.of( "America/Edmonton" ) ;
ZonedDateTime
决定了时区问题后,捕捉在该时区中看到的当前时刻。
ZonedDateTime now = ZonedDateTime.now( zoneId ) ;
从该时刻提取一天中的时间值。
LocalTime currentTimeOfDay = now.toLocalTime() ;
LocalTime
对象与您的目标开始-结束进行比较。
通常最好使用半开方法来定义时间跨度,其中开始是包容性的,而结束是独占性的。
if(
( ! currentTimeOfDay.isBefore( start ) ) // "Is not before …" is a briefer way of asking "Is equal to or later than …".
&&
currentTimeOfDay.isBefore( end )
)
{ … within target times }
else
{ … outside target times }
如果您必须通过解析标准ISO8601格式的文本来获取开始和结束时间值,其中的小时是24小时制(0-23)并使用填充零,则只需调用LocalTime.parse
方法。无需指定格式化模式。不需要指定Locale
..。
LocalTime start = LocalTime.parse( "03:19" ) ;
LocalTime end = LocalTime.parse( "03:21" ) ;
要防止错误输入,请为DateTimeParseException
.
LocalTime start = null ;
LocalTime end = null ;
try
{
start = LocalTime.parse( "03:19" ) ;
end = LocalTime.parse( "03:21" ) ;
}
catch ( DateTimeParseException e )
{
… handle faulty input
}
发布于 2021-02-25 05:37:44
问题是你正在比较你的两个固定日期。你检查一下13:00是在14:00之后。这将永远是真的。
您想要比较的是当前时间。您希望当前时间在开始时间之后,结束时间之前。
从java8开始,还建议使用LocalTime
而不是Date
..。这解决了你在时区等方面可能遇到的很多问题。
要获得当前时间,您可以使用LocalTime.now()
..。代码片段如下所示
LocalTime now = LocalTime.now();
now.isAfter(startTime) && now.isBefore(endTime);
https://stackoverflow.com/questions/66358680
复制相似问题