Theo
Theo

Reputation: 3149

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.

Upvotes: 0

Views: 362

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1501163

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.)

Upvotes: 4

Related Questions