检查我的当前时间是存在于晚上7:00到上午10:00之间的java android



示例:我的当前时间=8:25 PM意味着当前时间在7:00 PM到10.00 AM之间;如果里面显示消息?

这是餐厅的时间限制。从晚上7:00到上午10:00,时间范围用户不能订购任何东西。

try {
// Start Time
String string1 = "07:00 PM";
Date time1 = new SimpleDateFormat("hh:mm a").parse(string1);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(time1);
calendar1.add(Calendar.DATE, 1);

// End Time
String string2 = "10:00 AM";
Date time2 = new SimpleDateFormat("hh:mm a").parse(string2);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(time2);
calendar2.add(Calendar.DATE, 1);

// Get Current Time
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
String currenttime = sdf.format(date);
Date d = new SimpleDateFormat("hh:mm a").parse(currenttime);
Calendar calendar3 = Calendar.getInstance();
calendar3.setTime(d);
calendar3.add(Calendar.DATE, 1);
Date x = calendar3.getTime();
if (x.after(calendar1.getTime()) && x.before(calendar2.getTime())) {
System.out.println("Not possible to order now");
}
else
{
System.out.println("YES POSSIBLE");
}
} catch (ParseException e) {
e.printStackTrace();
}

如果您想避免NullPointerException&ParseException检查:

public static boolean isAvailableForBooking() {
/* 10:00 AM */
final int OPEN_HOUR = 10; /* 0 - 23*/
final int OPEN_MINUTE = 0; /* 0 - 59*/
final int OPEN_SECOND = 0; /* 0 - 59*/
/* 07:00 PM */
final int CLOSED_HOUR = 19;
final int CLOSED_MINUTE = 0;
final int CLOSED_SECOND = 0;
Calendar openHour = Calendar.getInstance();
openHour.set(Calendar.HOUR_OF_DAY, OPEN_HOUR);
openHour.set(Calendar.MINUTE, OPEN_MINUTE);
openHour.set(Calendar.SECOND, OPEN_SECOND);
Calendar closedHour = Calendar.getInstance();
closedHour.set(Calendar.HOUR_OF_DAY, CLOSED_HOUR);
closedHour.set(Calendar.MINUTE, CLOSED_MINUTE);
closedHour.set(Calendar.SECOND, CLOSED_SECOND);
Calendar now = Calendar.getInstance();
return now.after(openHour) && now.before(closedHour);
}

tl;dr

使用现代java.timeLocalTime

( ! localTime.isBefore( LocalTime.of( 19 , 0 ) ) )  // Is not before the start… (meaning, is equal to or later than)
&&                                                  // …and… 
localTime.isBefore( LocalTime.of( 7 , 0 ) ) ;       // is before the end.

java.time

切勿使用CalendarDate类。这些糟糕的类在几年前被JSR310中定义的现代java.time类所取代。

LocalTime start = LocalTime.of( 19 , 0 ) ;  // 7 PM.
LocalTime end = LocalTime.of( 10 , 0 ) ;    // 10 AM.

确定当前时间需要一个时区。在任何一个特定的时刻,一天中的时间和日期都会因地区而异。

如果要使用JVM的当前默认时区,请调用ZoneId.systemDefault()

ZoneId z = ZoneId.of( "Africa/Casablanca" ) ; 
LocalTime localTime = LocalTime.now( z ) ;

询问当前时间是否等于或晚于开始和结束前。提示:另一种询问"等于或晚于"的方式是"不在之前"。

boolean withinTimeRange = ( ! localTime.isBefore( start ) ) && localTime.isBefore( end ) ;

对于26之前的早期Android,将ThreeTenABP库添加到您的项目中,以使用几乎相同的API获得大部分java.time功能。

如果您尝试了任何代码,请以其他方式发布。您可以通过在日历对象上设置当前时间以及服务开始时间和结束时间来检查它,然后从日历中获取Date对象,并可以比较这些日期。

String SERVICE_START_TIME="202-05-04 19:00:00";
String SERVICE_END_TIME="202-05-05 10:00:00";
public static boolean isValidTime() {
try {
Date time1 = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss",
Locale.getDefault()).parse(SERVICE_START_TIME);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(time1);
calendar1.add(Calendar.DATE, 1);
Date time2 = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss",
Locale.getDefault()).parse(SERVICE_END_TIME);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(time2);
calendar2.add(Calendar.DATE, 1);

Date d = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss",
Locale.getDefault()).parse(getCurrentTime());
Calendar calendar3 = Calendar.getInstance();
calendar3.setTime(d);
calendar3.add(Calendar.DATE, 1);
Date now = calendar3.getTime();
if (now.after(calendar1.getTime()) && now.before(calendar2.getTime())) {
return true;
}
} catch (ParseException e) {
e.printStackTrace();
}
return false;
}

使用此功能可以获取系统当前时间。

private static String getCurrentTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss",
Locale.getDefault());
return sdf.format(new Date()).toUpperCase();
}

更简单的解决方案是采用以毫秒为单位的时间界限。然后以毫秒为单位计算所需时间,并在下方进行检查Bound<期望时间<上限。

最新更新