Java当前时间方法需要递增

时间:2013-11-16 21:30:57

标签: java date

我创建了一个获取当前时间的方法,我希望每次调用此方法时增加60秒或1分钟。任何帮助将不胜感激。

//我正在使用的当前方法

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());

5 个答案:

答案 0 :(得分:1)

首先,您需要在首次调用方法时“捕捉”当前时间。在后续调用中将添加一秒的偏移量。然后,只记录调用方法的次数,并将其作为以秒为单位的偏移量应用:

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());
}

只是说明显而易见,我们必须将调用它的次数(1,2,3)转换为以毫秒为单位的偏移量。因此,我们乘以1000得到1秒,然后60得到几分钟。

第一次调用该方法时,offset为0.因此,在第一次调用该方法时,它会增加0秒/毫秒。

答案 1 :(得分:1)

如果您的意思是:从当前时间开始并在每次调用函数时添加一分钟,解决方案需要两个部分:

  • 记住第一次调用它的当前时间(构造函数处理它)
  • 从该点开始添加一分钟(使用类变量)

    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());
    }
    }
    

答案 2 :(得分:0)

使用类似

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

答案 3 :(得分:0)

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());
}

答案 4 :(得分:0)

Joda-Time 2.3中,调用plusMinutes()方法。

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

jwa接受的答案使用静态影响整个应用程序。也许原始海报想要(不清楚)。如果要为多个窗口或多个用户或多个线程(例如使用servlet)中的每一个进行不同的递增,则定义一个类以跟踪初始时间并跟踪递增。按照他们的方式,每次通话的增量 - 每分钟呼叫似乎是一个奇怪的要求,所以也许你的问题需要重新编写或解释。

/**
 * 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() );
    }
}

跑步时......

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

我省略了一些现实问题,例如指定时区而不是依赖默认值,以及如果可以在多个线程上调用实例,则会出现线程安全问题。