比如两个日期:2005-12-31和2006-01-05,怎么才能精确的计算出这两个日期之间差的天数?

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

class Test2 {
public static String getDate(){
SimpleDateFormat ft = new SimpleDateFormat("yyyy/MM/dd");
Date dd = new Date();
return ft.format(dd);
}
public static long getQuot(String time1, String time2){
long quot = 0;
SimpleDateFormat ft = new SimpleDateFormat("yyyy/MM/dd");
try {
Date date1 = ft.parse( time1 );
Date date2 = ft.parse( time2 );
quot = date1.getTime() - date2.getTime();
quot = quot / 1000 / 60 / 60 / 24;
} catch (ParseException e) {
e.printStackTrace();
}
return quot;
}
public static void main(String[] args) throws Exception {
String date1 = "2008/8/8";
String date2 = getDate();
long day = getQuot(date1,date2);
System.out.println( "距离 "+date1+" 还有 "+day+" 天" );
}
}

你看一下这段代码,是从今天到2008/8/8的天数。
Date的getTime()方法返回自1970年1月1日午夜(通用时间)以来的毫秒数。
用2008/8/8的getTime()减去今天的getTime()就是这两天相差的毫秒数,1秒=1000毫秒,1分钟=60秒,1小时=60分钟,1天=24小时,然后除除除就得到天数了。

 

java怎么计算出 某年某月 到 某年某月之间的总月数 ?

//思路 (不考虑开发语言的因素)
// 月份只差其实就是,年份只差 乘以12 然后再加上 月份只差, 比如 2009-09 ----2012-02
// 这个就应该 (2012-2009 )*12 + (2-9)=36 +(-7)=29
//思路有了直接使用java套就可以了
public static void main(String[] args) throws ParseException {
//字符串格式化
DateFormat df = new SimpleDateFormat("yyyy-MM");

//字符串装换为Calendar
Calendar c= Calendar.getInstance();
c.setTime(df.parse("2009-01"));
//字符串装换为Calendar
Calendar d= Calendar.getInstance();
d.setTime(df.parse("2012-01"));
//计算年份只差 乘以12 然后加上月份之差
int m=(d.get(Calendar.YEAR)-c.get(Calendar.YEAR))*12+d.get(Calendar.MONTH)-

c.get(Calendar.MONTH);
System.out.println(m);
}

另外怎么 计算某年某月的天数 ?

题目:计算某年某月的天数
要求:通过键盘接收指定的年和月,判断该月的天数。
要点:月份可分为大月和小月,而二月份的天数跟是否闰年有关,所以对二月份要特殊处理。

 
  
  1. class DayTest { 
  2.     public static void main(String[] args) throws IOException { 
  3.         int year = -1; 
  4.         int month = -1; 
  5.         Scanner in = new Scanner(System.in); 
  6.         System.out.println("请输入年"); 
  7.         year = in.nextInt(); 
  8.         System.out.println("请输入月"); 
  9.         month = in.nextInt(); 
  10.         System.out.println(year + "年" + month + "月有" + days(year, month) + "天"); 
  11.     } 
  12.     public static int days(int year, int month) { 
  13.         int days = 0
  14.         if (month != 2) { 
  15.             switch (month) { 
  16.             case 1: 
  17.             case 3: 
  18.             case 5: 
  19.             case 7: 
  20.             case 8: 
  21.             case 10: 
  22.             case 12: 
  23.                 days = 31
  24.                 break; 
  25.             case 4: 
  26.             case 6: 
  27.             case 9: 
  28.             case 11: 
  29.                 days = 30
  30.             } 
  31.         } else { 
  32.             if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) 
  33.                 days = 29
  34.             else 
  35.                 days = 28
  36.         } 
  37.         return days; 
  38.     } 

另附一简单方法:

 
  
  1. import java.text.ParseException; 
  2. import java.text.SimpleDateFormat; 
  3. import java.util.Calendar; 
  4. import java.util.Date; 
  5. import java.util.GregorianCalendar; 
  6.  
  7. /** * @author crazy.j * @since 2007-12-17 * @version 1.0 */ 
  8. public class CalendarTester { 
  9.     private static SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月"); 
  10.  
  11.     /** * @param args */ 
  12.     public static void main(String[] args) { 
  13.         String source = "2007年2月"
  14.         try { 
  15.             Date date = format.parse(source); 
  16.             Calendar calendar = new GregorianCalendar(); 
  17.             calendar.setTime(date); 
  18.             System.out 
  19.                     .println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); 
  20.         } catch (ParseException e) { 
  21.             e.printStackTrace(); 
  22.         } 
  23.     }