Solved

Adding 15 min to current time in java

Posted on 2009-05-05
8
636 Views
Last Modified: 2012-05-06
Hi,

I am working in java

I want to add 15 minutes to current time

How do we do that

Please guide

Many Thanks
0
Comment
Question by:tia_kamakshi
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24302918
Date fifteenLater = new Date(System.currentTimeMilllis() + (100*60*60*15));
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24302920
Oops - typo
Date fifteenLater = new Date(System.currentTimeMilllis() + (1000*60*60*15));

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24302924
... and get rid of the extra 'l' in Millis ;-)
0
 
LVL 92

Assisted Solution

by:objects
objects earned 166 total points
ID: 24302942
you use the Calendar class to do any date arithmetic, you'll find an example here

http://helpdesk.objects.com.au/java/how-do-i-add-a-specified-number-of-hours-to-a-java-date

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 166 total points
ID: 24302987
You can save the original 'now' if you need it as a reference. The following prints

Tue May 05 11:04:14 BST 2009
Tue May 05 11:19:14 BST 2009


on my system
        Date now = new Date();

        Date fifteenLater = new Date(now.getTime() + (1000L*60*15));

        System.out.println(now);

        System.out.println(fifteenLater);

Open in new window

0
 
LVL 8

Accepted Solution

by:
ozlevanon earned 168 total points
ID: 24303162
I believe the best way is to use Calendar class. Of course you can use Date or directly using System. See examples in the code below.
			// Using calendar calss

			GregorianCalendar now = new GregorianCalendar();

			now.add(Calendar.MINUTE, 15);

			Date fifteenMinutesAway = now.getTime();

			long fifteenMinutesAwayInMilliseconds = now.getTime().getTime();
 

			// Using Date or System.currentTimeMillis()

			Date date1 = new Date(System.currentTimeMillis() + 15 * 60 * 1000);

			Date date2 = new Date(new Date().getTime() + 15 * 60 * 1000);

Open in new window

0
 

Author Closing Comment

by:tia_kamakshi
ID: 31577935
Many Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24312266
:-)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now