Danielle McKay
Danielle McKay

Reputation: 23

Check current time against specific times in Java and Android Studio

I want to set a boolean value isBasePriceTime to false if the current time is equal to 6pm or after 6pm and is not between midnight and 6am the next day. However it keeps setting the isBasePriceTime to false if the current time of the day is for example 2pm.

private boolean checkCurrentTimePriceType()
{
    /*Get the current price type depending on the time of the day the user wants to
      get a cab */

    Calendar today = Calendar.getInstance();
    Date currentDate = today.getTime();

    //creates a time for 6pm
    String nightPriceFullTime = "18:00:00";
    Date nightPriceTime = java.sql.Time.valueOf(nightPriceFullTime);
    Calendar nightPriceDateCalendar = Calendar.getInstance();
    nightPriceDateCalendar.setTime(nightPriceTime);

    //creates a time for midnight
    String nightPriceFullTimeMidnight = "00:00:00";
    Date nightPriceTimeMidnight = java.sql.Time.valueOf(nightPriceFullTimeMidnight);
    Calendar nightPriceMidnightDateCalendar = Calendar.getInstance();
    nightPriceMidnightDateCalendar.setTime(nightPriceTimeMidnight);

    //creates a time for 6am
    String basePriceFullTime = "06:00:00";
    Date basePriceTime = java.sql.Time.valueOf(basePriceFullTime);
    Calendar basePriceDateCalendar = Calendar.getInstance();
    basePriceDateCalendar.setTime(basePriceTime);


    boolean isBasePriceTime;

    //checks if the current time is or after 6pm, or if the the current time is between midnight and 6am
    if(((today.getTime().after(nightPriceDateCalendar.getTime())) || (today.getTime().equals(nightPriceDateCalendar.getTime())))
            || ((today.getTime().before(basePriceDateCalendar.getTime())) && (today.getTime().after(nightPriceMidnightDateCalendar.getTime()))))
    {
        //user will pay a night time price
        isBasePriceTime = false;
    }
    else
    {
        //user will pay a base time price
        isBasePriceTime = true;
    }

    //return value of isNightPrice boolean variable
    return isBasePriceTime;
}

Upvotes: 2

Views: 6031

Answers (3)

Zircon
Zircon

Reputation: 4707

Maybe this simplification will work for you:

int currentHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY); //Current hour
return currentHour < 18 //False if after 6pm

Upvotes: 5

Titus
Titus

Reputation: 22474

java.sql.Time.valueOf(nightPriceFullTime) will return January 1, 1970 18:00:00. You should use some other way to convert the Strings into dates. If you want to keep using this conversion method, you will have to change how you compare the time of the different calendars. For example:

if(nightPriceDateCalendar.get(Calendar.HOUR_OF_DAY) > today.get(Calendar.HOUR_OF_DAY) && ...){...}

Another approach will be this:

String time = "18:00:00";
SimpleDateFormat sdf = SimpleDateFormat("yyyy/MM/dd");
String onlyDate = sdf.format(new Date());
sdf = SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date dateAndTime = sdf.parse(onlyDate+" "+time);

Calendar nightPriceDateCalendar = Calendar.getInstance();
nightPriceDateCalendar.setTime(dateAndTime);

Upvotes: 0

Andy Turner
Andy Turner

Reputation: 140318

Using Jodatime:

LocalTime time = new LocalTime(/* Optional but recommended: specify timezone */);
return time.isAfter(LocalTime.of(18, 0))
    || time.isBefore(LocalTime.of(6, 0));

Using Java 8:

LocalTime time = LocalTime.now(/* Optional but recommended: specify timezone */);
return time.isAfter(LocalTime.of(18, 0))
    || time.isBefore(LocalTime.of(6, 0));

(Note that the class names are the same, but they are from different packages).

Upvotes: 2

Related Questions