• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to work out days weeks months years between dates ?  RSS feed

 
Ray mann
Ranch Hand
Posts: 48
Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
so i would like to know how to work out days weeks months years between dates ? Is there a java class that can do this for me as there are not 30 days in every year and not 365 days in every year.
This is my attempt so far, i know there is a not exactly 4 weeks in a months !  what is the correct way to do this ?  All help and advice is greatly appreciated.






Thanks in advance !
 
Tim Moores
Saloon Keeper
Posts: 4034
94
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The java.time API (available from Java 8 onwards) has all kinds of goodies you can use, but for the basic days-between-dates calculation check out the neat formula called Zeller's congruence.
 
Paul Clapham
Sheriff
Posts: 22834
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're really going to try seriously to answer that question, you're going to need to decide what it means before you get too far along. For example:

How many months are there between December 30, 2016 and January 2, 2017? That could plausibly be zero, one, or two depending on what rules you decide to apply. You can easily think up similar test cases for weeks and years, but they would all likely have the same sort of answer depending on what you do with partial weeks, months, and years.
 
Ray mann
Ranch Hand
Posts: 48
Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved the problem with the following method, here is the code :



Thanks guys!
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice to see how two local dates can form a period
There are however all sorts of things which could be improved there.
Why have you got those if blocks which do nothing? You can change them all to whatever is in the else part.
Don't print an empty String: what is the point of it. Don't use \n unless somebody has told you they require LF. Use println instead.
It is probably better not to use *s in import statements.
 
Ray mann
Ranch Hand
Posts: 48
Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made some changes to the code:



Thanks for the advice, I realized there was no point to the else block doing nothing, as thats the same as having no else block vice versa.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some further advice, albeit more of the nit picking kind.

The Java naming convention is to start variable names with a lowercase letter so 'CurrentLocalDate' should be 'currentLocalDate'.
Be consistent in your formatting: you've added spaces in some places and not others eg if (p.getYears()==1) and if(p.getMonths()==1)
Be consistent in your code: you've changed the structure of the if statements for the last case (days) which means it does something slightly different to the other 2, ie the first 2 if statements ignore negative values the last one prints out a negative value.

Also you may want to add special case handling for if the period is negative or the two dates are exactly the same.
 
Ray mann
Ranch Hand
Posts: 48
Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are the further changes made to the code :



Thanks for the advice and criticism, all valuable!  
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a lot of repetition in that code. You are testing for == 1 thrice. That shou‍ld factored out into a method by itself.
 
Ray mann
Ranch Hand
Posts: 48
Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Factored repeated if block to its own method :

Very Clever...
Thanks. I am learning from experienced devs here feeling fortunate!       
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
You can shorten that method with the ?: operator.
Don't use System.exit. Use some if‑elses and let the method terminate normally instead.
 
Ray mann
Ranch Hand
Posts: 48
Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome thanks further changes :

 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are making life hard for yourself by not using the methods of LocalDate which allow you to compare them. Have a look at the API documentation.
But your code is improving every time.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or you could do:
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!