Android/Java - Date Difference in days

前端 未结 18 1213
感动是毒
感动是毒 2020-11-22 14:17

I am getting the current date (in format 12/31/1999 i.e. mm/dd/yyyy) as using the below code:

Textview txtViewData;
txtViewDate.setText(\"Today is \" +
              


        
18条回答
  •  暖寄归人
    2020-11-22 14:59

    This is Simple and best calculation for me and may be for you.

           try {
                /// String CurrDate=  "10/6/2013";
                /// String PrvvDate=  "10/7/2013";
                Date date1 = null;
                Date date2 = null;
                SimpleDateFormat df = new SimpleDateFormat("M/dd/yyyy");
                date1 = df.parse(CurrDate);
                date2 = df.parse(PrvvDate);
                long diff = Math.abs(date1.getTime() - date2.getTime());
                long diffDays = diff / (24 * 60 * 60 * 1000);
    
    
                System.out.println(diffDays);
    
            } catch (Exception e1) {
                System.out.println("exception " + e1);
            }
    

提交回复
热议问题