Difference in days between two dates in Java?

后端 未结 19 1955
爱一瞬间的悲伤
爱一瞬间的悲伤 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 08:47

    I did it this way. it's easy :)

    Date d1 = jDateChooserFrom.getDate();
    Date d2 = jDateChooserTo.getDate();
    
    Calendar day1 = Calendar.getInstance();
    day1.setTime(d1);
    
    Calendar day2 = Calendar.getInstance();
    day2.setTime(d2);
    
    int from = day1.get(Calendar.DAY_OF_YEAR);
    int to = day2.get(Calendar.DAY_OF_YEAR);
    
    int difference = to-from;
    

提交回复
热议问题