-1

I want to get days between today and birthday:

Today code:

Date currentDate = Calendar.getInstance().getTime();
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        String currentString = formatter.format(currentDate);
        return currentString;

Birthday Code:

 String date =  dayOfMonth + "/" + month + "/" + year;
    datePicker1.setText(date);        
    Calendar calendar1 = Calendar.getInstance();
    calendar1.set(Calendar.MONTH,month +1);
    calendar1.set(Calendar.YEAR,year);
    calendar1.set(Calendar.DAY_OF_MONTH,dayOfMonth);        
    CharSequence datecharSequence = DateFormat.format("dd/MM/yyyy",calendar1);

Calculation Code:

 try {
           LocalDate date1 = LocalDate.now();
           LocalDate date2 = usrBirthDate();
           long diff = DAYS.between(date1, date2);
           Annotation.setText((int) diff);
       }
       catch (Exception exception){
           Toast.makeText(this, "Unable", Toast.LENGTH_SHORT).show();
       }
T.Kopa
  • 13
  • 4
  • What version of java do you use? If it is 8+ you'll probably benefit from using newer date\time API: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html – Monsieur Merso Apr 18 '21 at 08:56
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. See if you either can use [desugaring](https://developer.android.com/studio/write/java8-support-table) or add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project, in order to use java.time, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Apr 18 '21 at 09:30
  • Welcome to Stack Overflow. Does your code work? What was your question, please? So that we can help you, please specify the desired result and paste any error messages or wrong results into your question. – Ole V.V. Apr 18 '21 at 09:32
  • Don’t try to process your dates as strings. Just like you process numbers as `int` or `double`, not as `String`, process your dates as `LocalDate` objects. – Ole V.V. Apr 18 '21 at 09:37
  • For far better ways of finding the difference, see the answer by sanjeevRm below or [my answer to the linked original question](https://stackoverflow.com/a/45169929/5772882). For what you did wrong apart from using all the wrong classes, edit your question and specify your errors, and I can reopen it so that someone may answer that part. – Ole V.V. Apr 18 '21 at 09:50

1 Answers1

1

efficient way to find difference in days is by using ChronoUnit.DAYS

import static java.time.temporal.ChronoUnit.DAYS;

LocalDate date1 = LocalDate.now(); // current date
LocalDate date2 = LocalDate.of(2000, Month.JULY, 29); //  user birth date
long diff = DAYS.between(date1, date2);
sanjeevRm
  • 1,541
  • 2
  • 13
  • 24
  • @sanjeevRm , i want to use user method: usrBirthDate(), not static. – T.Kopa Apr 18 '21 at 13:47
  • @user15358848, date1 - today() date2 - usrBirthDate() – T.Kopa Apr 18 '21 at 13:48
  • you can assign usrBirthDate to date2 , `date2 = usrBirthDate();` usrBirthDate has to return LocaDate – sanjeevRm Apr 18 '21 at 14:04
  • @sanjeevRm, i've maded changes, but no result: `LocalDate date1 = LocalDate.now(); LocalDate date2 = usrBirthDate(); long diff = DAYS.between(date1, date2); Annotation.setText((int) diff);` – T.Kopa Apr 18 '21 at 15:35
  • can you share the are values of date1, date2 and result of diff, – sanjeevRm Apr 18 '21 at 16:10
  • This is in log error: 2021-04-18 20:28:25.416 ? E/whetstone.activity: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Boolean.booleanValue()' on a null object reference at com.miui.whetstone.server.WhetstoneActivityManagerService.enableMiuiStandby(WhetstoneActivityManagerService.java:406) at com.miui.whetstone.server.IWhetstoneActivityManager$Stub.onTransact(IWhetstoneActivityManager.java:682) at android.os.Binder.execTransactInternal(Binder.java:1021) at android.os.Binder.execTransact(Binder.java:994) – T.Kopa Apr 18 '21 at 16:34
  • please check this https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it/218510#218510 – sanjeevRm Apr 18 '21 at 16:39
  • @sanjeevRm , seems it's value format problem: data1 - 2021-04-18; data2 - 20/5/2016 – T.Kopa Apr 18 '21 at 16:40
  • you can convert format of date1 **DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/YYYY"); formatter.format(date1);** with this both date1 and date2 will be in **dd/MM/YYYY** – sanjeevRm Apr 18 '21 at 16:54
  • format changed, but still same error. i'll read link you've sent @sanjeevRm tnx – T.Kopa Apr 18 '21 at 17:47