Java日历复制

时间:2016-12-08 00:29:33

标签: java bukkit

我正在制作一个Elapsed Timer,除了当计时器到达0:59时它会跳到2:00而不是1:00这个事实非常好。不知道它是在10:00或几小时内做同样的事情,但我不知道为什么会这样做。

public long time() {
    long ends = System.nanoTime();
    return ends - starts;
}

public int time(TimeUnit unit) {
    return (int) unit.convert(time(), TimeUnit.NANOSECONDS);
}

public String toHoursMinutesSeconds() {
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

    Calendar now = Calendar.getInstance();
    now.set(Calendar.HOUR_OF_DAY, time(TimeUnit.HOURS));
    now.set(Calendar.MINUTE, time(TimeUnit.MINUTES));
    now.set(Calendar.SECOND, time(TimeUnit.SECONDS));

    return sdf.format(now.getTime());
}

1 个答案:

答案 0 :(得分:3)

如果您使用的是Java 8或更高版本,则可以利用新的Date and Time库。

public class EnhancedTimer {
    static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("HH:mm:ss");
    private final long initialTime;

    public EnhancedTimer() {
        initialTime = System.nanoTime();
    }

    public long elapsedTime() {
        return System.nanoTime() - initialTime;
    }

    public String toString() {
        return formatTime(elapsedTime());
    }

    public static String formatTime(long timestamp) {
        return LocalTime.ofNanoOfDay(timestamp).format(DATE_FORMAT);
    }
}

用法

import java.util.concurrent.TimeUnit;

public class TimerTest {
    public static void main(String[] args) {
        EnhancedTimer timer = new EnhancedTimer();
        long sleepDelay = 5000; // Sleep every 5 seconds.

        try {
            while (true) {
                System.out.printf("%s (%d)%n", timer, timer.elapsedTime());
                Thread.sleep(sleepDelay);
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            e.printStackTrace();
        }
    }
}

原始回复

  

以下是我原来的回复,以防有人仍然无法访问Java 8。

您需要设置从最重要到最不重要的值。

例如:天→小时→分钟→秒→等等。

否则值会复合。我写了一个基于原始代码的好Timer。我刚做了一些调整。主要方法将打印经过的时间,然后睡眠5秒钟。重复此过程直到程序被杀死。

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;

public class Timer {
    protected static final DateFormat DATE_FORMAT = new SimpleDateFormat("HH:mm:ss");
    protected static final TimeUnit UNIT = TimeUnit.NANOSECONDS;
    private final long initialTime;

    private static final long NANOSECONDS_IN_DAY = TimeUnit.DAYS.toNanos(1);
    private static final long NANOSECONDS_IN_HOUR = TimeUnit.HOURS.toNanos(1);
    private static final long NANOSECONDS_IN_MINUTE = TimeUnit.MINUTES.toNanos(1);

    public Timer() {
        initialTime = System.nanoTime();
    }

    public long elapsedTime() {
        return System.nanoTime() - initialTime;
    }

    public String toString() {
        return formatTime(elapsedTime(), UNIT, DATE_FORMAT);
    }

    private static final int nanosecondsToHours(long nanoseconds) {
        return (int) UNIT.toHours(nanoseconds % NANOSECONDS_IN_DAY);
    }

    private static final int nanosecondsToMinutes(long nanoseconds) {
        return (int) UNIT.toMinutes(nanoseconds % NANOSECONDS_IN_HOUR);
    }

    private static final int nanosecondsToSeconds(long nanoseconds) {
        return (int) UNIT.toSeconds(nanoseconds % NANOSECONDS_IN_MINUTE);
    }

    public static String formatTime(long timestamp, TimeUnit unit, DateFormat dateFormat) {
        Calendar cal = Calendar.getInstance();

        cal.set(Calendar.HOUR_OF_DAY, nanosecondsToHours(timestamp));
        cal.set(Calendar.MINUTE, nanosecondsToMinutes(timestamp));
        cal.set(Calendar.SECOND, nanosecondsToSeconds(timestamp));

        return dateFormat.format(cal.getTime());
    }
}

用法

import java.util.concurrent.TimeUnit;

public class TimerTest {
    public static void main(String[] args) {
        testTime();
        timeLoop();
    }

    private static void testTime() {
        long hours_23      = TimeUnit.HOURS.toNanos(23);
        long mins_55       = TimeUnit.MINUTES.toNanos(55);
        long secs_55       = TimeUnit.SECONDS.toNanos(55);
        long time_23_55_55 = hours_23 + mins_55 + secs_55;

        System.out.println(Timer.formatTime(time_23_55_55, Timer.UNIT, Timer.DATE_FORMAT)); // 23:55:55
    }

    private static void timeLoop() {
        Timer timer = new Timer();
        long sleepDelay = 5000; // Sleep every 5 seconds.

        try {
            while (true) {
                System.out.printf("%s (%d)%n", timer, timer.elapsedTime());
                Thread.sleep(sleepDelay);
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            e.printStackTrace();
        }
    }
}