Java Data Type How to - Increment current time








Question

We would like to know how to increment current time.

Answer

import java.text.SimpleDateFormat;
import java.util.Calendar;
//w  w w.j  av a 2  s. c  o m
public class Main{
  private static int offset;
  private static long firstCall = System.currentTimeMillis();

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

  public static String currentTime()
  {
      Calendar cal = Calendar.getInstance();
      cal.setTimeInMillis(firstCall + (offset * 1000 * 60));
      SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
      offset++;
      return sdf.format(cal.getTime());
  }
}

The code above generates the following result.