Reputation: 21893
final Date now = new Date();
Calendar c = Calendar.getInstance();
final Calendar cal = Calendar.getInstance();
cal.setTime(now);
cal.add(Calendar.HOUR_OF_DAY, 3);
SetTime(c.getTimeInMillis());
getTimeInMillis returns the same time I have it. not +3 hours
How do I add hours to current time correctly?
Upvotes: 1
Views: 484
Reputation: 93862
You're adding 3 hours to another calendar instance.
Try :
final Date now = new Date();
final Calendar cal = Calendar.getInstance();
cal.setTime(now);
cal.add(Calendar.HOUR_OF_DAY, 3);
SetTime(cal.getTimeInMillis());
As Peter Lawrey and Brian Roach stated, there's no need to create a Date object. So you could just do :
final Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR_OF_DAY, 3);
SetTime(cal.getTimeInMillis());
Upvotes: 4