Nayeem Shiddiki Abir
Nayeem Shiddiki Abir

Reputation: 27

Check my current time is exist between 7:00 PM -10.00 AM in java android

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();
    }

Upvotes: 1

Views: 1535

Answers (4)

Basil Bourque
Basil Bourque

Reputation: 338835

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.

Upvotes: 1

etomun
etomun

Reputation: 134

Here if you want to avoid NullPointerException & ParseException checking:

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);
    }

Upvotes: 1

Samir Spahic
Samir Spahic

Reputation: 561

Simpler solution would be to take time bounds in milliseconds. Then take your desired time in milliseconds and do the check lowerBound < desiredTime < upperBound.

Upvotes: 0

Rehan Sarwar
Rehan Sarwar

Reputation: 1004

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();
}

Upvotes: 0

Related Questions