Difference in days between two dates in Java?

后端 未结 19 1732
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 08:26

I need to find the number of days between two dates: one is from a report and one is the current date. My snippet:

  int age=calculateDiffer         


        
19条回答
  •  礼貌的吻别
    2020-11-22 09:01

    I use this funcion:

    DATEDIFF("31/01/2016", "01/03/2016") // me return 30 days
    

    my function:

    import java.util.Date;
    
    public long DATEDIFF(String date1, String date2) {
            long MILLISECS_PER_DAY = 24 * 60 * 60 * 1000;
            long days = 0l;
            SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy"); // "dd/MM/yyyy HH:mm:ss");
    
            Date dateIni = null;
            Date dateFin = null;        
            try {       
                dateIni = (Date) format.parse(date1);
                dateFin = (Date) format.parse(date2);
                days = (dateFin.getTime() - dateIni.getTime())/MILLISECS_PER_DAY;                        
            } catch (Exception e) {  e.printStackTrace();  }   
    
            return days; 
         }
    

提交回复
热议问题