Scott
Scott

Reputation: 1310

Java Timestamp - Adding five minutes

looking for some help with a bit of Java code i'm working on, i have the following code which prints out the date and time:

  Date dNow = new Date( ); // Instantiate a Date object
  SimpleDateFormat ft = new SimpleDateFormat ("MMM d, yyyy k:mm:ss");  // Time at server

Result: Mar 15, 2013 10:19:48

I'm creating a javascript counter to work from this number and countdown from 5 minutes. So i need to add 5 minues to the current date time in Java.

So, if the current date time is: Mar 15, 2013 10:19:48

I need to add 5 minutes to Java so that it prints out: Mar 15, 2013 10:24:48

Any ideas?

Upvotes: 34

Views: 116107

Answers (8)

Marko Topolnik
Marko Topolnik

Reputation: 200168

Instead of starting with

new Date()

start with

new Date(System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(5))

This will give you a Date instance that represents your required point in time. You don't need to change any other part of your code.

Upvotes: 67

Chandu D
Chandu D

Reputation: 501

You can try this one best performance

    GregorianCalendar gc = new GregorianCalendar();
            gc.setTimeInMillis(System.currentTimeMillis());
            gc.add(Calendar.MINUTE, -5);
            System.out.println(new java.util.Date().getTime());
            System.out.println(new java.util.Date(gc.getTime().getTime()).getTime());

Upvotes: 0

Basil Bourque
Basil Bourque

Reputation: 338644

tl;dr

Instant.now()
       .plusSeconds( TimeUnit.MINUTES.toSeconds( 5 ) )
       .toString()

2017-01-23T03:11:53.763Z

Details

The other Answers are outdated as of Java 8. The troublesome old date-time classes are now legacy, supplanted by the java.time classes.

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

Instant instant = Instant.now();

This class can do math such as adding a number of seconds. We can use the TimeUnit enum to covert our desired five minutes into a number of seconds.

long seconds = TimeUnit.MINUTES.toSeconds( 5 );
Instant fiveMinutesLater = instant.plusSeconds( seconds );

To generate a string in standard ISO 8601 format, call toString.

String output = fiveMinutesLater.toString();

To generate strings in other formats, use the ZonedDateTime class and DateTimeFormatter class. Search Stack Overflow for many examples and discussions of those classes.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Upvotes: 8

Alexandre Santos
Alexandre Santos

Reputation: 8338

Ignoring Dates and focusing on the question.

My preference is to use java.util.concurrent.TimeUnit since it adds clarity to my code.

In Java,

long now = System.currentTimeMillis();

5 minutes from now using TimeUtil is:

long nowPlus5Minutes = now + TimeUnit.MINUTES.toMillis(5);

Reference: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/TimeUnit.html

Upvotes: 25

subodh
subodh

Reputation: 6158

Date dNow = new Date(System.currentTimeMillis()+5*60*1000)
SimpleDateFormat ft = new SimpleDateFormat ("MMM d, yyyy k:mm:ss");
System.out.println(ft.format(dNow));

with the help of deprecated method getMinutes(),setMinutes(int)

 Date dNow = new Date( ); // Instantiate a Date object
 int mm = dNow.getMinutes();
 dNow.setMinutes(mm+5);

Upvotes: 2

PermGenError
PermGenError

Reputation: 46408

You should use Calendar class to manipulate Date and time:

The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week

  Date dNow = new Date( ); // Instantiate a Date object
  Calendar cal = Calendar.getInstance();
  cal.setTime(dNow);
  cal.add(Calendar.MINUTE, 5);
  dNow = cal.getTime();

Upvotes: 16

Christophe Roussy
Christophe Roussy

Reputation: 16999

Java dates use Unix time in milliseconds. So you either calculate how much 5 minutes are in milliseconds and add them to your date or use the Calendar class which does it for you.

Upvotes: 0

Sudhanshu Umalkar
Sudhanshu Umalkar

Reputation: 4202

Use this ...

    Calendar calendar = Calendar.getInstance();
    System.out.println(calendar.getTime());
    calendar.add(Calendar.MINUTE, 5);
    System.out.println(calendar.getTime());

Upvotes: 4

Related Questions