永发信息网

java 程序中判断过去的某一天离现在相差多少天的程序

答案:2  悬赏:20  手机版
解决时间 2021-02-15 21:27
  • 提问者网友:临风不自傲
  • 2021-02-14 20:50
java 程序中判断过去的某一天离现在相差多少天的程序
最佳答案
  • 五星知识达人网友:人類模型
  • 2021-02-14 21:09
java可以使用计算日期的天数差,以下是详细代码:

import java.text.ParseException;  
import java.text.SimpleDateFormat;  
import java.util.Calendar;  
import java.util.Date;  
public class test16 {  
      
    public static void main(String[] args) throws ParseException {  
        // TODO Auto-generated method stub  
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        Date d1=sdf.parse("2012-09-08 10:10:10");  
        Date d2=sdf.parse("2012-09-15 00:00:00");  
        System.out.println(daysBetween(d1,d2));  

        System.out.println(daysBetween("2012-09-08 10:10:10","2012-09-15 00:00:00"));  
    }  

        
    public static int daysBetween(Date smdate,Date bdate) throws ParseException    
    {    
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
        smdate=sdf.parse(sdf.format(smdate));  
        bdate=sdf.parse(sdf.format(bdate));  
        Calendar cal = Calendar.getInstance();    
        cal.setTime(smdate);    
        long time1 = cal.getTimeInMillis();                 
        cal.setTime(bdate);    
        long time2 = cal.getTimeInMillis();         
        long between_days=(time2-time1)/(1000*3600*24);  

       return Integer.parseInt(String.valueOf(between_days));           
    }    

  
    public static int daysBetween(String smdate,String bdate) throws ParseException{  
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
        Calendar cal = Calendar.getInstance();    
        cal.setTime(sdf.parse(smdate));    
        long time1 = cal.getTimeInMillis();                 
        cal.setTime(sdf.parse(bdate));    
        long time2 = cal.getTimeInMillis();         
        long between_days=(time2-time1)/(1000*3600*24);  

       return Integer.parseInt(String.valueOf(between_days));     
    }  

}
全部回答
  • 1楼网友:一秋
  • 2021-02-14 21:41
搜一下:java 程序中判断过去的某一天离现在相差多少天的程序
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯