Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do calendar arithmetic with java.util.Date?

Tags:

java

Currently I have a Date object representing a time. How would I add 5 minutes to this object?

like image 782
deltanovember Avatar asked May 05 '11 08:05

deltanovember


People also ask

How do I set the current date in Java Util calendar?

Use: String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss"). format(Calendar. getInstance().

Which Java package is used for date calendar?

Calendar class in Java is an abstract class that provides methods for converting date between a specific instant in time and a set of calendar fields such as MONTH, YEAR, HOUR, etc.


1 Answers

You could use Calendar, which will make it easy to add any length of time:

Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.MINUTE, 5); Date newDate = cal.getTime(); 

For your case you could just add the time in milliseconds like this:

Date newDate = new Date(date.getTime() + 5 * 60 * 1000L); 
like image 57
WhiteFang34 Avatar answered Oct 05 '22 17:10

WhiteFang34