• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Check the difference of the date's month

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a start date and an end date, I want to check if the start date is 6 month earlier than the end date. How should I do that? I have the if else statement at the end, but I am not sure how to fix this if else statement. Help will be appreciated


 
Marshal
Posts: 79701
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

If at all possible stop using Calendar and Date. Use the new date‑time classes. Otherwise you will have to do things likeThat is horrible code. I am sure somebody else will find a better way to add six months.
 
Hin Wong
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, thank you for your reply. I am not sure if I did it the right way, but what I did is I calculate the difference between two date and than use that value to compare it. Something like this, but it seems it doesn't work

Here is my code

 
Campbell Ritchie
Marshal
Posts: 79701
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Converting things to Strings and back looks like the wrong way to do it. You cannot simply take month numbers and see whether they differ by 6; if you go from now to next March the month numbers will be 7 and 2 which differ by 5. And the wrong way, even though the difference is 7 months. That is why you should get rid of the old time and date classes. You can use local dates to create a Duration object, and I am sure you can work out whether that Duration is six months or more. You can create a Period object equivalent to six months and then compare that to the duration object you have from earlier.

But these classes are only available in Java8.

Don't use underscores_in_variable_names, only in CONSTANT_NAMES. Use camelCase for variableNames.
 
When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic