0

this is an extension post to my old post, which can not be solved yet: android diff days between two dates

i would like to have the number of difference days between 2 dates. for this i using this code:

                DatePicker datepicker= (DatePicker) findViewById(R.id.datepicker);

                Log.e("-->",""+datepicker.getMonth());
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.YEAR, datepicker.getYear());
                calendar.set(Calendar.MONTH, datepicker.getMonth());
                calendar.set(Calendar.DATE, datepicker.getDayOfMonth());
                calendar.set(Calendar.HOUR, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                long dpDate = calendar.getTimeInMillis();


                Calendar Today = Calendar.getInstance();
                Today.set(Calendar.YEAR, Today.get(Calendar.YEAR));
                Today.set(Calendar.MONTH, Today.get(Calendar.MONTH));
                Today.set(Calendar.DATE, Today.get(Calendar.DATE));
                Today.set(Calendar.HOUR_OF_DAY, 0);
                Today.set(Calendar.MINUTE, 0);
                Today.set(Calendar.SECOND, 0);
                Date date = Today.getTime();


                Log.e("-->", "cal1: " + calendar.getTime());
                Log.e("-->", "cal2: " + Today.getTime());


                long diff = calendar.getTimeInMillis() - Today.getTimeInMillis();
                long days = diff / (24 * 60 * 60 * 1000);

                Log.e("-->", "days: " + days);
                Log.e("-->", "====================");

And this is my Log: Can you finde the mistake?

i don't understand it!

Community
  • 1
  • 1
Trombone0904
  • 4,132
  • 8
  • 51
  • 104
  • Have a look at the answer by mad_troll here: http://stackoverflow.com/questions/3299972/difference-in-days-between-two-dates-in-java or just use jodatime. – kha Dec 01 '15 at 10:42
  • Use joda library http://stackoverflow.com/a/9318287/1790537 – Nas Dec 01 '15 at 10:43
  • okay i have i try with this code: http://stackoverflow.com/a/3300078/5068746 but android studio tells me, that date is deprecated. which is the new way to realize it? – Trombone0904 Dec 01 '15 at 12:14
  • okay now i try the code from kha and it works :) – Trombone0904 Dec 01 '15 at 13:38

0 Answers0