在java中获取日期之间的差异

Cha*_*lla 34 java date

可能重复:
如何使用java计算两个日期之间的差异

我正在尝试这样的事情,我试图从组合框中获取日期

Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();  

int Sdate=Integer.parseInt(cmbSdate.getSelectedItem().toString());  
int Smonth=cmbSmonth.getSelectedIndex();
int Syear=Integer.parseInt(cmbSyear.getSelectedItem().toString());  

int Edate=Integer.parseInt(cmbEdate.getSelectedItem().toString());
int Emonth=cmbEmonth.getSelectedIndex();
int Eyear=Integer.parseInt(cmbEyear.getSelectedItem().toString());

start.set(Syear,Smonth,Sdate);  
end.set(Eyear,Emonth,Edate);

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String startdate=dateFormat.format(start.getTime());  
String enddate=dateFormat.format(end.getTime());
Run Code Online (Sandbox Code Playgroud)

我无法减去结束日期和开始日期如何获得开始日期和结束日期之间的差异?

hd4*_*d42 43

Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
start.set(2010, 7, 23);
end.set(2010, 8, 26);
Date startDate = start.getTime();
Date endDate = end.getTime();
long startTime = startDate.getTime();
long endTime = endDate.getTime();
long diffTime = endTime - startTime;
long diffDays = diffTime / (1000 * 60 * 60 * 24);
DateFormat dateFormat = DateFormat.getDateInstance();
System.out.println("The difference between "+
  dateFormat.format(startDate)+" and "+
  dateFormat.format(endDate)+" is "+
  diffDays+" days.");
Run Code Online (Sandbox Code Playgroud)

当穿过夏令时(或闰秒)时,这将无效,因为orange80指出并且在使用不同时间时可能不会给出预期结果.使用JodaTime可能更容易获得正确的结果,因为在我知道之前使用普通Java的唯一正确方法是使用Calendar的add和before/after方法来检查和调整计算:

start.add(Calendar.DAY_OF_MONTH, (int)diffDays);
while (start.before(end)) {
    start.add(Calendar.DAY_OF_MONTH, 1);
    diffDays++;
}
while (start.after(end)) {
    start.add(Calendar.DAY_OF_MONTH, -1);
    diffDays--;
}
Run Code Online (Sandbox Code Playgroud)

  • 注意跨越夏令时边界的日期范围将不正确. (9认同)
  • 您能为我解释一下这段代码吗:1000 * 60 * 60 * 24 (2认同)
  • 我遇到了同样的问题,但我发现冗长的代码,我认为这些代码非常无效.虽然这是一个重复和旧的,我发现一个简单的方法尝试这个`public static long getTimeDiff(Date dateOne,Date dateTwo){long diff = 0; long timeDiff = Math.abs(dateOne.getTime() - dateTwo.getTime()); diff = TimeUnit.MILLISECONDS.toDays(timeDiff); 返回差异; }` (2认同)
  • @abhilash以start.set(2014,Calendar.MARCH,8)和end.set(2014,Calendar.APRIL,8)为例; 当我这样做时(3月30日在德国穿越夏令时,3月9日在美国穿过夏令时),我得到了30天的差异,没有得到橙色80评论的启发和正确的31天.如果您在Locale设置为India的计算机上运行此程序,则可能没有任何区别,因为印度似乎没有夏令时(根据http://www.timeanddate.com/time/dst/2014.html) ) (2认同)

Fai*_*roz 24

为此使用JodaTime.它比标准的Java DateTime Apis要好得多.以下是JodaTime中用于计算天数差异的代码:

private static void dateDiff() {

    System.out.println("Calculate difference between two dates");
    System.out.println("=================================================================");

    DateTime startDate = new DateTime(2000, 1, 19, 0, 0, 0, 0);
    DateTime endDate = new DateTime();

    Days d = Days.daysBetween(startDate, endDate);
    int days = d.getDays();

    System.out.println("  Difference between " + endDate);
    System.out.println("  and " + startDate + " is " + days + " days.");

  }
Run Code Online (Sandbox Code Playgroud)

  • 谢谢先生的答案.费萨尔但我只需要使用内置函数和apis (2认同)
  • 既然 Java 8 具有与 Joda Time 基本相同的构造,您可以使用 java.time.LocalDateTime 和 ChronoUnit.DAYS.between(startDate,endDate); 来实现这一点。 (2认同)

ric*_*ics 14

这样.

import java.util.Date;
import java.util.GregorianCalendar;

/**
 * DateDiff -- compute the difference between two dates.
 */
public class DateDiff {
  public static void main(String[] av) {
    /** The date at the end of the last century */
    Date d1 = new GregorianCalendar(2000, 11, 31, 23, 59).getTime();

    /** Today's date */
    Date today = new Date();

    // Get msec from each, and subtract.
    long diff = today.getTime() - d1.getTime();

    System.out.println("The 21st century (up to " + today + ") is "
        + (diff / (1000 * 60 * 60 * 24)) + " days old.");
  }
Run Code Online (Sandbox Code Playgroud)

}

是一篇关于Java日期算术的文章.