Leandros
Leandros

Reputation: 16825

Calendar return wrong time

I use a calendar for the alarm in my app, but if I use the calendar, the timeInMillis are wrong.
I show you an example:

// Get Current Time
Calendar calNow = Calendar.getInstance();
calNow.setTimeInMillis(System.currentTimeMillis());
calNow.add(Calendar.HOUR, calNow.get(Calendar.HOUR_OF_DAY));
calNow.add(Calendar.MINUTE, calNow.get(Calendar.MINUTE));
long now = calNow.getTimeInMillis();

long now returns for example: 1345848049917
now must be the same as System.currentTimeMillis(), but it isn't, system returns 1345846849921

now is in my timezone: 25.08.2012 - 00:40:49
System.currentTimeMillis() is: 25.08.2012 - 00:20:49

Exactly 20 Minutes later, but why?

Upvotes: 0

Views: 662

Answers (1)

Keppil
Keppil

Reputation: 46239

I'm not quite sure what you were trying to do, but these rows

calNow.add(Calendar.HOUR, calNow.get(Calendar.HOUR_OF_DAY));
calNow.add(Calendar.MINUTE, calNow.get(Calendar.MINUTE));

doubles the hours and minutes of the day, turning 00:20:49 into 00:40:49.

Upvotes: 3

Related Questions