两个日期相差天数计算

来源:互联网 发布:java jdbc学生管理 编辑:程序博客网 时间:2024/05/01 20:57
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Datesub {
   public static void main(String[] args) throws ParseException {
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
   Date d = sdf.parse("2007-03-01"); 
   Calendar calendar = Calendar.getInstance();
   calendar.setTime(new Date());
   long timethis = calendar.getTimeInMillis();
   calendar.setTime(d);
   long timeend = calendar.getTimeInMillis();
   long theday = (timeend - timethis) / (1000 * 60 * 60 * 24);
   System.out.print(theday);
 }

}

  1. import java.text.ParseException;  
  2. import java.text.SimpleDateFormat;  
  3. import java.util.Calendar;  
  4. import java.util.Date;  

  5. public class test16 {  
  6.     /** 
  7.      * @param args 
  8.      * @throws ParseException  
  9.      */  
  10.     public static void main(String[] args) throws ParseException {  
  11.         // TODO Auto-generated method stub  
  12.         SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  13.         Date d1=sdf.parse("2012-09-08 10:10:10");  
  14.         Date d2=sdf.parse("2012-09-15 00:00:00");  
  15.         System.out.println(daysBetween(d1,d2));  
  16.   
  17.         System.out.println(daysBetween("2012-09-08 10:10:10","2012-09-15 00:00:00"));  
  18.     }  
  19.       
  20.     /**  
  21.      * 计算两个日期之间相差的天数  
  22.      * @param smdate 较小的时间 
  23.      * @param bdate  较大的时间 
  24.      * @return 相差天数 
  25.      * @throws ParseException  
  26.      */    
  27.     public static int daysBetween(Date smdate,Date bdate) throws ParseException    
  28.     {    
  29.         SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
  30.         smdate=sdf.parse(sdf.format(smdate));  
  31.         bdate=sdf.parse(sdf.format(bdate));  
  32.         Calendar cal = Calendar.getInstance();    
  33.         cal.setTime(smdate);    
  34.         long time1 = cal.getTimeInMillis();                 
  35.         cal.setTime(bdate);    
  36.         long time2 = cal.getTimeInMillis();         
  37.         long between_days=(time2-time1)/(1000*3600*24);  
  38.             
  39.        return Integer.parseInt(String.valueOf(between_days));           
  40.     }    
  41.       
  42. /** 
  43. *字符串的日期格式的计算 
  44. */  
  45.    public static int daysBetween(String smdate,String bdate) throws ParseException{  
  46.         SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
  47.         Calendar cal = Calendar.getInstance();    
  48.         cal.setTime(sdf.parse(smdate));    
  49.         long time1 = cal.getTimeInMillis();                 
  50.         cal.setTime(sdf.parse(bdate));    
  51.         long time2 = cal.getTimeInMillis();         
  52.         long between_days=(time2-time1)/(1000*3600*24);  
  53.             
  54.        return Integer.parseInt(String.valueOf(between_days));     
  55.     }  
  56.   
  57. }  

原创粉丝点击