Trying to calculate the date between Employee's age(period), with the date they were hired(LocalDate). How...












0














I'm practicing using encapsulation in java to create an Employees profile. I'm trying to include both the employees age - calculated using



private Period age;
private LocalDate currentDate;
private LocalDate dob;
public Period calcAge() {
currentDate = LocalDate.now();
age = Period.between(currentDate, dob); //dob is Date Of Birth
return age;
}


, and the age at which they were hired. My first thoughts were to accomplish this with the following method:



public Period hiredAge() {
return Period.between(age, hireDate);
}


However, I receive the error "Period cannot be converted to LocalDate"



I've tried to research both a way to perform the calculation using "age" as a LocalDate - and by inputting hireDate as a Period, both to no avail. How can I calculate the age at which the Employee was hired?










share|improve this question


















  • 2




    I'm practicing using encapsulation: OK, but then start by understanding the difference between a field and a local variable. A field is part of the state of an object. It's normal for an employee to have a date f birth. But an employee shouldn't have a currentDate. That should be a local variable. And since the age can be computed from the date of birth, and since it keeps changing, it should be a local variable, too.
    – JB Nizet
    Nov 23 at 0:18






  • 1




    nit: Do you really want to represent age as a Period instead of an int or float?
    – Krease
    Nov 23 at 0:23










  • Why do you not simply use ChronoUnit.YEARS.between(dob, currentDate)?
    – Meno Hochschild
    Nov 23 at 11:07
















0














I'm practicing using encapsulation in java to create an Employees profile. I'm trying to include both the employees age - calculated using



private Period age;
private LocalDate currentDate;
private LocalDate dob;
public Period calcAge() {
currentDate = LocalDate.now();
age = Period.between(currentDate, dob); //dob is Date Of Birth
return age;
}


, and the age at which they were hired. My first thoughts were to accomplish this with the following method:



public Period hiredAge() {
return Period.between(age, hireDate);
}


However, I receive the error "Period cannot be converted to LocalDate"



I've tried to research both a way to perform the calculation using "age" as a LocalDate - and by inputting hireDate as a Period, both to no avail. How can I calculate the age at which the Employee was hired?










share|improve this question


















  • 2




    I'm practicing using encapsulation: OK, but then start by understanding the difference between a field and a local variable. A field is part of the state of an object. It's normal for an employee to have a date f birth. But an employee shouldn't have a currentDate. That should be a local variable. And since the age can be computed from the date of birth, and since it keeps changing, it should be a local variable, too.
    – JB Nizet
    Nov 23 at 0:18






  • 1




    nit: Do you really want to represent age as a Period instead of an int or float?
    – Krease
    Nov 23 at 0:23










  • Why do you not simply use ChronoUnit.YEARS.between(dob, currentDate)?
    – Meno Hochschild
    Nov 23 at 11:07














0












0








0







I'm practicing using encapsulation in java to create an Employees profile. I'm trying to include both the employees age - calculated using



private Period age;
private LocalDate currentDate;
private LocalDate dob;
public Period calcAge() {
currentDate = LocalDate.now();
age = Period.between(currentDate, dob); //dob is Date Of Birth
return age;
}


, and the age at which they were hired. My first thoughts were to accomplish this with the following method:



public Period hiredAge() {
return Period.between(age, hireDate);
}


However, I receive the error "Period cannot be converted to LocalDate"



I've tried to research both a way to perform the calculation using "age" as a LocalDate - and by inputting hireDate as a Period, both to no avail. How can I calculate the age at which the Employee was hired?










share|improve this question













I'm practicing using encapsulation in java to create an Employees profile. I'm trying to include both the employees age - calculated using



private Period age;
private LocalDate currentDate;
private LocalDate dob;
public Period calcAge() {
currentDate = LocalDate.now();
age = Period.between(currentDate, dob); //dob is Date Of Birth
return age;
}


, and the age at which they were hired. My first thoughts were to accomplish this with the following method:



public Period hiredAge() {
return Period.between(age, hireDate);
}


However, I receive the error "Period cannot be converted to LocalDate"



I've tried to research both a way to perform the calculation using "age" as a LocalDate - and by inputting hireDate as a Period, both to no avail. How can I calculate the age at which the Employee was hired?







java methods encapsulation period localdate






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 at 0:09









ddalcanto

274




274








  • 2




    I'm practicing using encapsulation: OK, but then start by understanding the difference between a field and a local variable. A field is part of the state of an object. It's normal for an employee to have a date f birth. But an employee shouldn't have a currentDate. That should be a local variable. And since the age can be computed from the date of birth, and since it keeps changing, it should be a local variable, too.
    – JB Nizet
    Nov 23 at 0:18






  • 1




    nit: Do you really want to represent age as a Period instead of an int or float?
    – Krease
    Nov 23 at 0:23










  • Why do you not simply use ChronoUnit.YEARS.between(dob, currentDate)?
    – Meno Hochschild
    Nov 23 at 11:07














  • 2




    I'm practicing using encapsulation: OK, but then start by understanding the difference between a field and a local variable. A field is part of the state of an object. It's normal for an employee to have a date f birth. But an employee shouldn't have a currentDate. That should be a local variable. And since the age can be computed from the date of birth, and since it keeps changing, it should be a local variable, too.
    – JB Nizet
    Nov 23 at 0:18






  • 1




    nit: Do you really want to represent age as a Period instead of an int or float?
    – Krease
    Nov 23 at 0:23










  • Why do you not simply use ChronoUnit.YEARS.between(dob, currentDate)?
    – Meno Hochschild
    Nov 23 at 11:07








2




2




I'm practicing using encapsulation: OK, but then start by understanding the difference between a field and a local variable. A field is part of the state of an object. It's normal for an employee to have a date f birth. But an employee shouldn't have a currentDate. That should be a local variable. And since the age can be computed from the date of birth, and since it keeps changing, it should be a local variable, too.
– JB Nizet
Nov 23 at 0:18




I'm practicing using encapsulation: OK, but then start by understanding the difference between a field and a local variable. A field is part of the state of an object. It's normal for an employee to have a date f birth. But an employee shouldn't have a currentDate. That should be a local variable. And since the age can be computed from the date of birth, and since it keeps changing, it should be a local variable, too.
– JB Nizet
Nov 23 at 0:18




1




1




nit: Do you really want to represent age as a Period instead of an int or float?
– Krease
Nov 23 at 0:23




nit: Do you really want to represent age as a Period instead of an int or float?
– Krease
Nov 23 at 0:23












Why do you not simply use ChronoUnit.YEARS.between(dob, currentDate)?
– Meno Hochschild
Nov 23 at 11:07




Why do you not simply use ChronoUnit.YEARS.between(dob, currentDate)?
– Meno Hochschild
Nov 23 at 11:07












1 Answer
1






active

oldest

votes


















0














Just like you calculated age using Period.between(currentDate, dob), you can calculate the age at which they were hired using Period.between(dob, hireDate).



BTW you are getting that error because you are passing age, which is a Period, not a LocalDate in Period.between(age, hireDate).






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439293%2ftrying-to-calculate-the-date-between-employees-ageperiod-with-the-date-they%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Just like you calculated age using Period.between(currentDate, dob), you can calculate the age at which they were hired using Period.between(dob, hireDate).



    BTW you are getting that error because you are passing age, which is a Period, not a LocalDate in Period.between(age, hireDate).






    share|improve this answer


























      0














      Just like you calculated age using Period.between(currentDate, dob), you can calculate the age at which they were hired using Period.between(dob, hireDate).



      BTW you are getting that error because you are passing age, which is a Period, not a LocalDate in Period.between(age, hireDate).






      share|improve this answer
























        0












        0








        0






        Just like you calculated age using Period.between(currentDate, dob), you can calculate the age at which they were hired using Period.between(dob, hireDate).



        BTW you are getting that error because you are passing age, which is a Period, not a LocalDate in Period.between(age, hireDate).






        share|improve this answer












        Just like you calculated age using Period.between(currentDate, dob), you can calculate the age at which they were hired using Period.between(dob, hireDate).



        BTW you are getting that error because you are passing age, which is a Period, not a LocalDate in Period.between(age, hireDate).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 at 0:15









        Kartik

        2,51731331




        2,51731331






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439293%2ftrying-to-calculate-the-date-between-employees-ageperiod-with-the-date-they%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            Alexandru Averescu

            Trompette piccolo