Frage

Wie man konvertieren System.currentTimeMillis(); bis Sekunden?

long start6=System.currentTimeMillis();
System.out.println(counter.countPrimes(100000000)+" for "+start6);

Die Konsole zeigt es mir 5761455 for 1307816001290. Ich kann nicht lesen, wie viele Sekunden das ist.

Irgendeine Hilfe?

War es hilfreich?

Lösung

long start = System.currentTimeMillis();
counter.countPrimes(1000000);
long end = System.currentTimeMillis();

System.out.println("Took : " + ((end - start) / 1000));

AKTUALISIEREN

Eine noch genauere Lösung wäre:

final long start = System.nanoTime();
counter.countPrimes(1000000);
final long end = System.nanoTime();

System.out.println("Took: " + ((end - start) / 1000000) + "ms");
System.out.println("Took: " + (end - start)/ 1000000000 + " seconds");

Andere Tipps

TimeUnit

Verwenden Sie das TimeUnit Auflauf in Java 5 und später eingebaut.

long timeMillis = System.currentTimeMillis();
long timeSeconds = TimeUnit.MILLISECONDS.toSeconds(timeMillis);

Like SO:

(int)(milliseconds / 1000)

Aus Ihrem Code scheint es, dass Sie versuchen, zu messen, wie lange eine Berechnung gedauert hat (im Gegensatz zum Versuch herauszufinden, was die aktuelle Zeit ist).

In diesem Fall müssen Sie anrufen currentTimeMillis Nehmen Sie vor und nach der Berechnung den Unterschied und teilen Sie das Ergebnis durch 1000, um Millisekunden in Sekunden umzuwandeln.

Java 8 bietet jetzt die prägnanteste Methode, um den aktuellen Unix -Zeitstempel zu erhalten:

Instant.now().getEpochSecond();

Ich habe den folgenden Code in meiner letzten Aufgabe geschrieben, er kann Ihnen helfen:

// A method that converts the nano-seconds to Seconds-Minutes-Hours form
private static String formatTime(long nanoSeconds)
{
    int hours, minutes, remainder, totalSecondsNoFraction;
    double totalSeconds, seconds;


    // Calculating hours, minutes and seconds
    totalSeconds = (double) nanoSeconds / 1000000000.0;
    String s = Double.toString(totalSeconds);
    String [] arr = s.split("\\.");
    totalSecondsNoFraction = Integer.parseInt(arr[0]);
    hours = totalSecondsNoFraction / 3600;
    remainder = totalSecondsNoFraction % 3600;
    minutes = remainder / 60;
    seconds = remainder % 60;
    if(arr[1].contains("E")) seconds = Double.parseDouble("." + arr[1]);
    else seconds += Double.parseDouble("." + arr[1]);


    // Formatting the string that conatins hours, minutes and seconds
    StringBuilder result = new StringBuilder(".");
    String sep = "", nextSep = " and ";
    if(seconds > 0)
    {
        result.insert(0, " seconds").insert(0, seconds);
        sep = nextSep;
        nextSep = ", ";
    }
    if(minutes > 0)
    {
        if(minutes > 1) result.insert(0, sep).insert(0, " minutes").insert(0, minutes);
        else result.insert(0, sep).insert(0, " minute").insert(0, minutes);
        sep = nextSep;
        nextSep = ", ";
    }
    if(hours > 0)
    {
        if(hours > 1) result.insert(0, sep).insert(0, " hours").insert(0, hours);
        else result.insert(0, sep).insert(0, " hour").insert(0, hours);
    }
    return result.toString();
}

Konvertieren Sie einfach Nanosekunden in Milli-Sekunden.

TimeUnit.SECONDS.convert(start6, TimeUnit.MILLISECONDS);

Für die Umwandlung von Millisekunden auf Sekunden, da 1 Sekunde = 10³ Millisekunden:

//here m will be in seconds
long m = System.currentTimeMillis()/1000;

//here m will be in minutes
long m = System.currentTimeMillis()/1000/60; //this will give in mins
// Convert millis to seconds. This can be simplified a bit,
// but I left it in this form for clarity.
long m = System.currentTimeMillis(); // that's our input
int s = Math.max(.18*(Math.toRadians(m)/Math.PI),Math.pow(Math.E, Math.log(m)-Math.log(1000)));
System.out.println( "seconds: "+s );
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top