2010-10-17 39 views
3

我確定以前用其他方式詢問過我無法找到,所以我很抱歉重複,如果我是。Java格式日期之間的詳細區別

我想計算兩個日期之間的差異,然後顯示天,分,秒的差異;像「3天23分59秒」

很容易,我敢肯定,但我是一個C#人的練習,所以我很難在這個盒子上開箱即用。

提前致謝!

回答

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); 
+0

事端的使用的數字錯誤。這是我想去的方向。 – BennyT 2010-10-17 04:38:22

+0

@BennyT,現在應該工作。 – 2010-10-17 04:46:47

+0

謝謝@Mark! – BennyT 2010-10-17 04:53:14

5

對於當前是標準Java API的一部分的Calendar和Date對象,沒有直接的做法。您可以找到兩個日期之間的毫秒數並使用算術。這取決於您的準確性需求,例如您是否需要考慮閏年。這會使計算更加混亂,但仍然可行。

Joda-Time庫提供了一個Period對象,它完全符合您所期待的。

+0

現在我會堅持喬達,內置日曆的東西足以讓你噁心。 – 2010-10-17 04:26:47

+0

我會,但包括我的應用程序的一個小部分的lib沒有多大意義,是嗎? – BennyT 2010-10-17 04:38:54

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