How to add minutes to my Date
The issue for you is that you are using mm
. You should use MM
. MM
is for month and mm
is for minutes. Try with yyyy-MM-dd HH:mm
Other approach:
It can be as simple as this (other option is to use joda-time)
static final long ONE_MINUTE_IN_MILLIS=60000;//millisecs
Calendar date = Calendar.getInstance();
long t= date.getTimeInMillis();
Date afterAddingTenMins=new Date(t + (10 * ONE_MINUTE_IN_MILLIS));
you can use DateUtils class in org.apache.commons.lang3.time package
int addMinuteTime = 5;
Date targetTime = new Date(); //now
targetTime = DateUtils.addMinutes(targetTime, addMinuteTime); //add minute
Convenience method for implementing @Pangea's answer:
/*
* Convenience method to add a specified number of minutes to a Date object
* From: http://stackoverflow.com/questions/9043981/how-to-add-minutes-to-my-date
* @param minutes The number of minutes to add
* @param beforeTime The time that will have minutes added to it
* @return A date object with the specified number of minutes added to it
*/
private static Date addMinutesToDate(int minutes, Date beforeTime){
final long ONE_MINUTE_IN_MILLIS = 60000;//millisecs
long curTimeInMs = beforeTime.getTime();
Date afterAddingMins = new Date(curTimeInMs + (minutes * ONE_MINUTE_IN_MILLIS));
return afterAddingMins;
}