Next 2 hours using Calendar object/Java Android

I want to capture the number of steps the user is doing for the next 2 hours.

This is what I am putting.

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);

Log.d("Calendar",calendar.getTime().toString());
long endTime = calendar.getTimeInMillis();
calendar.add(Calendar.HOUR_OF_DAY,2);
long startTime = calendar.getTimeInMillis();

However the app is crashing giving me the following exception.

java.lang.IllegalStateException: Invalid end time: 1432119600355

Any ideas? Thanks.

Jon Skeet
people
quotationmark

Look at what you're doing:

long endTime = calendar.getTimeInMillis();
calendar.add(Calendar.HOUR_OF_DAY,2);
long startTime = calendar.getTimeInMillis();

So your start time is two hours after your end time. That's not going to be valid. I suspect you just need to change the ordering:

long startTime = calendar.getTimeInMillis();
calendar.add(Calendar.HOUR_OF_DAY,2);
long endTime = calendar.getTimeInMillis();

That will now have a start time of midnight and an end time of 2am. (Although you haven't cleared the millisecond part, so it'll be "some time in the first second after midnight" etc.)

people

See more on this question at Stackoverflow