Java格式日期之间的详细差异

时间:2010-10-17 04:09:01

标签: java date formatting

我确信以前曾以其他方式询问过我无法找到的内容,所以如果我愿意的话,我会提前道歉。

我正在尝试计算两个日期之间的差异,然后以天,分和秒显示差异;像“3天23分59秒”这样的东西

很容易,我很确定,但是我通过练习成为C#的家伙,所以我很难在这个方面开箱即用。

提前致谢!

3 个答案:

答案 0 :(得分:5)

对于当前属于标准Java API的Calendar和Date对象,没有直接的做法。您可以找到两个日期之间的毫秒数并使用算术。这取决于您的准确性需求,例如您是否需要考虑闰年。这会使计算更加混乱,但仍然可行。

Joda-Time库提供的Period对象完全符合您的目标。

答案 1 :(得分:3)

Date start = new Date(2010, 10, 13);
Date end   = new Date(2010, 10, 18);

long diffInMillis = end.getTime() - start.getTime();

long diffInDays  = diffInMillis/1000/86400;
long diffInHours = (diffInMillis/1000 - 86400*diffInDays) / 3600;
long diffInMins  = (diffInMillis/1000 - 86400*diffInDays - 3600*diffInHours) / 60;
long diffInSecs  = (diffInMillis/1000 - 86400*diffInDays - 3600*diffInHours - 60*diffInMins);

答案 2 :(得分:0)

这就是我所做的,它对我有用,但我确信它可以改进甚至还没有,也许有一个隐藏的(对我而言)这样做...

/*Imports*/

package javatest;
import java.util.Date;
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        long segundo = 1000;
        long minuto = segundo * 60;
        long hora = minuto * 60;
        long dia = hora * 24;
        long semana = dia * 7;
        long mes = dia * 30;
        long anio = mes * 12;

        long startDate = new Date().getTime();
        //startDate = startDate - (semana + hora + 30 * minuto + anio*2);
        startDate = startDate - (mes);
        long endDate = new Date().getTime();

        System.out.println("Inicio: " + new Date(startDate));
        System.out.println("Final: " + new Date(endDate));

        System.out.println(PrettifyDateDiff(endDate - startDate, true));
    }

    private static String PrettifyDateDiff(long dateDiff, boolean showDisclaimer) {
        /* Constantes utilizadas para facilitar
         * la lectura de la aplicación
         */

        long second = 1000;
        long minute = second * 60;
        long hour = minute * 60;
        long day = hour * 24;
        long week = day * 7;
        long month = day * 30;
        long year = month * 12;

        // Dividimos los milisegundos entre su equivalente de
        // las constantes de arriba para obtener el valor en la
        // escala de tiempo correspondiente.
        long minutes = dateDiff / minute;
        long hours = dateDiff / hour;
        long days = dateDiff / day;
        long weeks = dateDiff / week;
        long months = dateDiff / month;
        long years = dateDiff / year;

        String prettyDateString = "";

        if (minutes > 60) {
            prettyDateString = minutes - (hours * 60) + " minutos.";

            if (hours > 24) {
                prettyDateString = hours - (days * 24) + " horas " + prettyDateString;

                if (days > 7) {
                    prettyDateString = days - (weeks * 7) + " dias " + prettyDateString;

                    if(weeks > 4){
                        prettyDateString = weeks - (months * 4)  + " semanas " + prettyDateString;

                        if(months > 12){

                            prettyDateString = months - (years * 12) + " meses " + prettyDateString;

                            if(years > 0){
                                prettyDateString = years + " años " + prettyDateString;
                            }
                        }else{
                            prettyDateString = months  + " meses " + prettyDateString;
                        }

                    }else{
                        prettyDateString = weeks + " semanas " + prettyDateString;
                    }
                } else {
                    prettyDateString = days + " dias " + prettyDateString;
                }
            } else {
                prettyDateString = hours + " horas " + prettyDateString;
            }

        } else {
            prettyDateString = minutes + " minutos.";
        }

        if(showDisclaimer && (weeks > 0 || months > 0)){
            prettyDateString += " (Semanas de 7 dias, Meses de 30 dias).";
        }

        return prettyDateString;
    }
}