Chc
Chc

Reputation: 47

Java current time method need to increment

I have created a method to get the current time, I would like the time to increment 60 seconds or 1 min every time I call this method. Any help would be appreciated.

// Current method I am using

public String currentTime() 
{
    Calendar cal = Calendar.getInstance();
    cal.getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    return sdf.format(cal.getTime());
}

// main

System.out.println("The time is now: " + d.currentTime());

Upvotes: 0

Views: 2326

Answers (5)

Basil Bourque
Basil Bourque

Reputation: 340300

In Joda-Time 2.3, call the plusMinutes() method.

org.joda.time.DateTime now = new org.joda.time.DateTime();
org.joda.time.DateTime future = now.plusMinutes(1);

The accepted answer by jwa uses a static which affects the entire app. Perhaps the original poster wants (not clear). If you want a different incrementing for each of multiple windows or multiple users or multiple threads (such as with servlets), then define a class to track the initial time and track the incrementing. By they way, this increment-a-minute-on-every-call seems like a strange requirement, so perhaps your questions needs to be reworded or explained.

/**
 * Created by Basil Bourque on 2013-11-16.
 * © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
 */
public class FutureClock {

    org.joda.time.DateTime startDateTime = new org.joda.time.DateTime();
    org.joda.time.DateTime incrementedDateTime = startDateTime;

    org.joda.time.DateTime increment() {
        // Increment the previous datetime by one minute. Remember new datetime.
        this.incrementedDateTime = this.incrementedDateTime.plusMinutes(1);
        return this.incrementedDateTime;
    }

    public static void main(String[] args)
    {
        FutureClock futureClock = new FutureClock();
        System.out.println( "Incremented minute: " + futureClock.increment() + " while true time is: " + new org.joda.time.DateTime() );
        System.out.println( "Incremented minute: " + futureClock.increment() + " while true time is: " + new org.joda.time.DateTime() );
        System.out.println( "Incremented minute: " + futureClock.increment() + " while true time is: " + new org.joda.time.DateTime() );
        System.out.println( "Incremented minute: " + futureClock.increment() + " while true time is: " + new org.joda.time.DateTime() );

        // Wait over a minute.
        System.out.println( "Please wait a minute…" );
        try {
            Thread.sleep(1000 * 70 );
        } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }

        System.out.println( "Next minute: " + futureClock.increment() + " while true time is: " + new org.joda.time.DateTime() );
    }
}

When run…

Incremented minute: 2013-11-16T22:44:19.571-08:00 while true time is: 2013-11-16T22:43:19.704-08:00
Incremented minute: 2013-11-16T22:45:19.571-08:00 while true time is: 2013-11-16T22:43:19.704-08:00
Incremented minute: 2013-11-16T22:46:19.571-08:00 while true time is: 2013-11-16T22:43:19.704-08:00
Incremented minute: 2013-11-16T22:47:19.571-08:00 while true time is: 2013-11-16T22:43:19.704-08:00
Please wait a minute…
Next minute: 2013-11-16T22:48:19.571-08:00 while true time is: 2013-11-16T22:44:29.705-08:00

I omitted some real-world concerns, such as specifying a time zone rather than rely on default, and thread-safety issues if an instance may be called on more than one thread.

Upvotes: 0

Christian Garbin
Christian Garbin

Reputation: 2532

If you mean: start with current time and add one minute every time the function is called, the solution needs two pieces:

  • Remember the current time when it is called for the first time (constructor takes care of that)
  • Add one minute from that point on (a class variable is used for that)

    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    
    public class TestJava {
    
    public static class GoIntoTheFuture {
    
        private int minutesToAdd = 0;
        private Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    
        public String currentTime() {
           cal.add(Calendar.MINUTE,minutesToAdd++);
           return sdf.format(cal.getTime());
        }
    }
    
    public static void main(String[] args) {
    
        GoIntoTheFuture g = new GoIntoTheFuture();
        for( int i = 0; i < 5; i ++ )
            System.out.println("The time is now: " + g.currentTime());
    }
    }
    

Upvotes: 1

jwa
jwa

Reputation: 3281

Firstly, you need to "snap" the current time when the method is first call. The one-second offsets will be added to that in subsequent calls. Then, simply record how many times the method has been called, and apply that as an offset in seconds:

static int offset;
static long firstCall;

public static void main(String[] args)
{
    System.out.println(currentTime());
    System.out.println(currentTime());
    System.out.println(currentTime());
}

public static String currentTime()
{
    if (offset == 0)
    {
        firstCall = System.currentTimeMillis();
    }

    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(firstCall + (offset * 1000 * 60));
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

    offset++;
    return sdf.format(cal.getTime());
}

Just to state the obvious, we have to turn the number of times this has been called (1, 2, 3) to an offset in milliseconds. Thus we multiply by 1000 to get to 1 second, and then 60 to get to minutes.

The first time the method is called, offset is 0. Thus it adds 0 seconds / milliseconds the first time the method is called.

Upvotes: 1

Muhammad Kashif Nazar
Muhammad Kashif Nazar

Reputation: 23965

public String currentTime() 
{
    Calendar cal = Calendar.getInstance();

    //add one minute to the current time.
    cal.add(Calendar.MINUTE, 1);

    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    return sdf.format(cal.getTime());
}

Upvotes: 0

Petr Mensik
Petr Mensik

Reputation: 27536

Use something like

Calendar cal = Calendar.getInstance();
cal.getTime();
cal.add(Calendar.MINUTE, 1);

Upvotes: 0

Related Questions