Turo
Turo

Reputation: 1607

Java - number of hours round up to 1

I'm working on an excercise about car parking. Once car is parked for even 5 minutes, it needs to pay for full 1 hour. So I'm looking to receive int 1 from code below, but I'm getting a 0. Some hints?

GregorianCalendar data1 = new GregorianCalendar(2015,1,4,11,10);
GregorianCalendar data2 = new GregorianCalendar(2015,1,4,11,20);   
long b =(data2.getTimeInMillis()-data1.getTimeInMillis())/1000/3600;
int k=(int)Math.ceil(b);
System.out.println(k); 

Upvotes: 1

Views: 491

Answers (6)

Vivek Singh
Vivek Singh

Reputation: 3649

using long b is making output to 0. Use double or float. Long data type is a 64-bit signed two's complement integer. So whatever you do in RHS it will result in an integer rounded-off. So lets say you perform:

System.out.println((long) 1.9);

output of it will be 1 instead of 1.9.

So try by doing

float b = (float) ((data2.getTimeInMillis() - data1.getTimeInMillis()) / 1000) / 3600;

Upvotes: 0

SubOptimal
SubOptimal

Reputation: 22973

Here a verbose and self-explaining example. It also handle the case if the endTime is before startTime (for what ever reason).

GregorianCalendar startTime = new GregorianCalendar(2015, 1, 4, 11, 10);
GregorianCalendar endTime = new GregorianCalendar(2015, 1, 4, 11, 20);
long parkingTimeMillis = startTime.getTimeInMillis() - endTime.getTimeInMillis();
long hoursToPay = TimeUnit.MILLISECONDS.toHours(parkingTimeMillis) + 1;
System.out.println(hoursToPay);

Upvotes: 0

Maksym Kreshchyshyn
Maksym Kreshchyshyn

Reputation: 354

Try to rewrite your code to something like that

GregorianCalendar data1 = new GregorianCalendar(2015,1,4,11,10);
GregorianCalendar data2 = new GregorianCalendar(2015,1,4,11,20);
// b must be float   
float b =(data2.getTimeInMillis()-data1.getTimeInMillis())/1000;
b = b /3600;
int k=(int)Math.ceil(b);
return k;

Upvotes: 1

benscabbia
benscabbia

Reputation: 18072

This should work:

GregorianCalendar data1 = new GregorianCalendar(2015,1,4,11,10);
GregorianCalendar data2 = new GregorianCalendar(2015,1,4,11,20);   
double b =(data2.getTimeInMillis()-data1.getTimeInMillis())/1000.0/3600.0;
double k=Math.ceil(b);
System.out.println(k);

You want to use Math.ceil() and work in double so you can store decimal places. Otherwise when you do subtraction and divide by 3600, you may end up with 450 / 3600 and with a long, you will end up with 0, and hence why you kept getting 0.

Upvotes: 2

Ankur Singhal
Ankur Singhal

Reputation: 26067

public static void main(String[] args) {
        GregorianCalendar data1 = new GregorianCalendar(2015, 1, 4, 11, 10);
        GregorianCalendar data2 = new GregorianCalendar(2015, 1, 4, 11, 20);
        double b = (data2.getTimeInMillis() - data1.getTimeInMillis()) / (1000.0 * 3600.0);
        System.out.println(b);
        int k = (int) Math.ceil(b);
        System.out.println(k);
    }

output

0.16666666666666666
1

brush your Math function here, really very well explained by the author.

Upvotes: 1

Jens
Jens

Reputation: 69440

Two thinks:

  1. b must be a double. Because if you devide the difference by 1000 and 3600 b is not langer a long value. it is a floting pot value (0.16666666666666666);
  2. Use Math.ceil instead of Math.floor.

Math.floor

Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer

this will return 0

Math.ceil

Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.

this will return 1 because 1 is the next bigger integer

Upvotes: 0

Related Questions