Java - Verstrichene Zeit in Tagen, Stunden, Minuten, Sekunden

Java - Zeit in Tagen, Stunden, Minuten, Sekunden

Zwei Java-Beispiele zeigen Ihnen, wie Sie die verstrichene Zeit im Format von Tagen, Stunden, Minuten und Sekunden drucken.

1. Standard-JDK-Datums-APIs

Sie müssen die verstrichene Zeit manuell berechnen.

DateTimeUtils.java

package com.example.dateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTimeUtils {

    public static void main(String[] args) {

      DateTimeUtils obj = new DateTimeUtils();
      SimpleDateFormat simpleDateFormat =
                new SimpleDateFormat("dd/M/yyyy hh:mm:ss");

      try {

        Date date1 = simpleDateFormat.parse("10/10/2013 11:30:10");
        Date date2 = simpleDateFormat.parse("13/10/2013 20:35:55");

        obj.printDifference(date1, date2);

      } catch (ParseException e) {
        e.printStackTrace();
      }

    }

    //1 minute = 60 seconds
    //1 hour = 60 x 60 = 3600
    //1 day = 3600 x 24 = 86400
    public void printDifference(Date startDate, Date endDate){

        //milliseconds
        long different = endDate.getTime() - startDate.getTime();

        System.out.println("startDate : " + startDate);
        System.out.println("endDate : "+ endDate);
        System.out.println("different : " + different);

        long secondsInMilli = 1000;
        long minutesInMilli = secondsInMilli * 60;
        long hoursInMilli = minutesInMilli * 60;
        long daysInMilli = hoursInMilli * 24;

        long elapsedDays = different / daysInMilli;
        different = different % daysInMilli;

        long elapsedHours = different / hoursInMilli;
        different = different % hoursInMilli;

        long elapsedMinutes = different / minutesInMilli;
        different = different % minutesInMilli;

        long elapsedSeconds = different / secondsInMilli;

        System.out.printf(
            "%d days, %d hours, %d minutes, %d seconds%n",
            elapsedDays,
            elapsedHours, elapsedMinutes, elapsedSeconds);

    }

}

Ausgabe

startDate : Thu Oct 10 11:30:10 SGT 2013
endDate : Sun Oct 13 20:35:55 SGT 2013
different : 291945000
3 days, 9 hours, 5 minutes, 45 seconds

How about years and months?
Es ist sehr mühsam, Jahr und Monat manuell zu berechnen. Schlagen Sie vor, die Joda-Zeitbibliothek zu verwenden, siehe nächstes Beispiel.

2. Joda Time Beispiel

Joda-time ist die bevorzugte Java-Bibliothek für Datum und Uhrzeit. Sie enthält APIs zur Berechnung der verstrichenen Zeit und zum Abrufen im Format Jahre, Monate, Tage, Stunden, Minuten und Sekunden.

DateTimeUtils.java

package com.example.dateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.joda.time.Interval;
import org.joda.time.Period;

public class DateTimeUtils {

    public static void main(String[] args) {

      DateTimeUtils obj = new DateTimeUtils();
      SimpleDateFormat simpleDateFormat =
               new SimpleDateFormat("dd/M/yyyy hh:mm:ss");

      try {

        Date date1 = simpleDateFormat.parse("10/10/2013 11:30:10");
        Date date2 = simpleDateFormat.parse("13/11/2014 20:35:55");

        obj.printDifference(date1, date2);

      } catch (ParseException e) {
        e.printStackTrace();
      }

    }

    public void printDifference(Date startDate, Date endDate){

      Interval interval =
               new Interval(startDate.getTime(), endDate.getTime());
      Period period = interval.toPeriod();

          System.out.printf(
           "%d years, %d months, %d days, %d hours, %d minutes, %d seconds%n",
           period.getYears(), period.getMonths(), period.getDays(),
           period.getHours(), period.getMinutes(), period.getSeconds());

    }
}

Ausgabe

1 years, 1 months, 3 days, 9 hours, 5 minutes, 45 seconds