6

Suppose I have : Employee model which has startDate as its property variable and Promotion model has promotionDate. I want to find out for how long employee has worked until his promotion for which I have to find difference between promotionDate and startDate. If I get startDate as employee.getStartDate() and promotionDate as promotion.getPromotionDate, how can I find difference in years months and days for any dates,

Any help would be really appreciated.

UPDATE : I SOLVED PROBLEM AS BELOW

String startDate = "2018-01-01";
String promotionDate = "2019-11-08";

LocalDate sdate = LocalDate.parse(startDate);
LocalDate pdate = LocalDate.parse(promotionDate);

LocalDate ssdate = LocalDate.of(sdate.getYear(), sdate.getMonth(), sdate.getDayOfMonth());
LocalDate ppdate = LocalDate.of(pdate.getYear(), pdate.getMonth(), pdate.getDayOfMonth());

Period period = Period.between(ssdate, ppdate);
System.out.println("Difference: " + period.getYears() + " years " 
                                  + period.getMonths() + " months "
                                  + period.getDays() + " days ");

Thank you.

rizius
  • 61
  • 1
  • 4
  • And what have you tried? – Nicholas K Mar 03 '19 at 10:09
  • Is that March 4 or 3 April? How would it be 2 months and 2 days?? In any case you need like `Period.between(LocalDate.of(2015, Month.JANUARY, 1), LocalDate.of(2018, Month.APRIL, 3))`. The example yields a period of `P3Y3M2D`, read as a period of 3 years 3 months 2 days. [Link to documentation of the `Period` class](https://docs.oracle.com/javase/9/docs/api/java/time/Period.html). – Ole V.V. Mar 03 '19 at 10:21
  • I recommend [this answer](https://stackoverflow.com/a/24048565/5772882) and [this one](https://stackoverflow.com/a/43795903/5772882) (the two are similar in substance). – Ole V.V. Mar 03 '19 at 10:27
  • 1
    Welcome to Stack Overflow. It‘s always a good idea to search, and search pretty thoroughly, before posting a question. Often you will find a good answer faster that way. – Ole V.V. Mar 03 '19 at 10:29
  • @NicholasK yes I have tried as per answer provided which solved my problem. – rizius Mar 05 '19 at 05:40
  • @OleV.V. yes you are right. Next time I'll keep that in mind. Thanks. – rizius Mar 05 '19 at 05:41

1 Answers1

7

Using LocalDate.of(int year, int month, int dayOfMonth) from java8 you can create two dates and find the difference:

LocalDate firstDate = LocalDate.of(2015, 1, 1);
LocalDate secondDate = LocalDate.of(2018, 3, 4);

Period period = Period.between(firstDate, secondDate);

Period has such methods as .getYears(), .getMonths() etc.

If you have java.util.Date objects instead of int values 2015, 1, 1, you can convert Date to LocalDate before:

LocalDate startLocalDate = startDate.toInstant()
        .atZone(ZoneId.systemDefault())
        .toLocalDate();
Ruslan
  • 6,090
  • 1
  • 21
  • 36
  • Previously I asked question in generalized way but I have updated the question as per my real problem. Here in your answer: LocalDate.of(2015, 1, 1) . instead of providing 2015, 1, 1 as actual values how can I find the diiference? – rizius Mar 03 '19 at 18:37
  • @rizius updated the answer – Ruslan Mar 03 '19 at 20:14
  • Thanks for the update. But if I have startDate as of type String , can I convert that into LocalDate ? – rizius Mar 05 '19 at 05:43