Android/Java - Date Difference in days

前端 未结 18 1175
感动是毒
感动是毒 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

    I found a very easy way to do this and it's what I'm using in my app.

    Let's say you have the dates in Time objects (or whatever, we just need the milliseconds):

    Time date1 = initializeDate1(); //get the date from somewhere
    Time date2 = initializeDate2(); //get the date from somewhere
    
    long millis1 = date1.toMillis(true);
    long millis2 = date2.toMillis(true);
    
    long difference = millis2 - millis1 ;
    
    //now get the days from the difference and that's it
    long days = TimeUnit.MILLISECONDS.toDays(difference);
    
    //now you can do something like
    if(days == 7)
    {
        //do whatever when there's a week of difference
    }
    
    if(days >= 30)
    {
        //do whatever when it's been a month or more
    }
    

提交回复
热议问题