英文:
Check my current time is exist between 7:00 PM -10.00 AM in java android
问题
try {
// 开始时间
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);
// 结束时间
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);
// 获取当前时间
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("现在无法下单");
} else {
System.out.println("可以下单");
}
} catch (ParseException e) {
e.printStackTrace();
}
英文:
Example: my current time = 8:25 PM it means the current time is inside 7:00 PM to 10.00 AM. So how can I determined it & if inside show a message?
It's for a restaurant time restriction. from 7:00 PM to 10.00 AM time range user can't order anything.
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();
}
答案1
得分: 1
如果你想避免 NullPointerException 和 ParseException 检查,可以使用以下代码:
<!-- 开始代码片段:js 隐藏:false 控制台:true babel:false -->
<!-- 语言:lang-js -->
public static boolean isAvailableForBooking() {
/* 上午 10:00 */
final int OPEN_HOUR = 10; /* 0 - 23*/
final int OPEN_MINUTE = 0; /* 0 - 59*/
final int OPEN_SECOND = 0; /* 0 - 59*/
/* 下午 07:00 */
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);
}
<!-- 结束代码片段 -->
英文:
Here if you want to avoid NullPointerException & ParseException checking:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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);
}
<!-- end snippet -->
答案2
得分: 1
tl;dr
使用现代的 java.time 类,LocalTime
。
( ! localTime.isBefore( LocalTime.of( 19 , 0 ) ) ) // 不早于开始时间…(意味着等于或晚于)
&& // …并且…
localTime.isBefore( LocalTime.of( 7 , 0 ) ) ; // 早于结束时间。
java.time
永远不要使用 Calendar
或 Date
类。这些糟糕的类早在多年前就被 JSR 310 中定义的现代 java.time 类所取代。
LocalTime start = LocalTime.of( 19 , 0 ) ; // 晚上7点。
LocalTime end = LocalTime.of( 10 , 0 ) ; // 上午10点。
确定当前时间需要一个时区。对于任何给定的时刻,全球各地的时间和日期会因时区而异。
如果想使用 JVM 的当前默认时区,请调用 ZoneId.systemDefault()
。
ZoneId z = ZoneId.of( "Africa/Casablanca" ) ;
LocalTime localTime = LocalTime.now( z ) ;
询问当前时间是否等于或晚于开始时间 且 早于结束时间。提示:另一种表达“等于或晚于”的方式是“不早于”。
boolean withinTimeRange = ( ! localTime.isBefore( start ) ) && localTime.isBefore( end ) ;
对于 Android 26 之前的早期版本,请将 ThreeTenABP 库添加到项目中,以获取几乎相同 API 的大部分 java.time 功能。
英文:
tl;dr
Use modern java.time class, LocalTime
.
( ! 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
Never use Calendar
or Date
classes. These terrible classes were supplanted years ago by the modern java.time classes defined in JSR 310.
LocalTime start = LocalTime.of( 19 , 0 ) ; // 7 PM.
LocalTime end = LocalTime.of( 10 , 0 ) ; // 10 AM.
Determining the current time requires a time zone. For any given moment, the time of day, and the date, varies around the globe by zone.
If you want to use the JVM’s current default time zone, call ZoneId.systemDefault()
.
ZoneId z = ZoneId.of( "Africa/Casablanca" ) ;
LocalTime localTime = LocalTime.now( z ) ;
Ask if the current time is equal to or later than the start and before the end. Tip: another way to ask “is equal to or later” is “is not before”.
boolean withinTimeRange = ( ! localTime.isBefore( start ) ) && localTime.isBefore( end ) ;
For early Android before 26, add the ThreeTenABP library to your project to get most of the java.time functionality with nearly the same API.
答案3
得分: 0
如果您尝试过任何代码,请将其发布;否则,您可以通过在 Calendar 对象上设置当前时间、服务启动时间和结束时间,然后从日历中获取 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();
}
英文:
If you tried any code then please post it otherwise, You can check it by setting your current time and your service start time and end time on a Calendar Object and then get Date object from the calendar and can compare these dates.
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;
}
You can get your system current time by using this function.
private static String getCurrentTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss",
Locale.getDefault());
return sdf.format(new Date()).toUpperCase();
}
答案4
得分: 0
更简单的解决方案是以毫秒为单位设定时间界限。然后将您所期望的时间转换为毫秒,并进行以下检查:lowerBound < desiredTime < upperBound。
英文:
Simpler solution would be to take time bounds in milliseconds. Then take your desired time in milliseconds and do the check lowerBound < desiredTime < upperBound.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论