• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

add to date and check versus today

 
Ranch Hand
Posts: 205
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am looking at trying to get a start date to add a certain amounts of month, then check to see if it is still in the future or not.

What I have so far...



The idea is I will provide a date, it will add the IDP length of time and then check to see if that day is either today or after. for some reason this does not seem to be working.
 
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using Date and Calendar still? Move onto the newer date classes.
You appear to be adding 0 to the months. Or have I read the code wrongly? Yes, I have got it wrong. You have an incorrect if because you can never get false out of it (line 6). You cannot tell from here what IDP is; it might for all we know be negative. And you have a date format which goes out of scope before you use it.
 
John Morgan
Ranch Hand
Posts: 205
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IDP will come from an outside source and will be an INT anywhere from 1-100, Typically 12,24,36,48,60,97,98,99 --- although there could be other amounts (they are the number of months).

We have not upgraded some of our legacy programs so this particular program is still running an older version of Java. My understanding the newer date class is for Java SE 8 and above.

Campbell Ritchie wrote:Are you using Date and Calendar still? Move onto the newer date classes.
You appear to be adding 0 to the months. Or have I read the code wrongly? Yes, I have got it wrong. You have an incorrect if because you can never get false out of it (line 6). You cannot tell from here what IDP is; it might for all we know be negative. And you have a date format which goes out of scope before you use it.

 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A bit off topic... Sorry for being picky.

1. Your method doing more things than method's name suggests it's suppose to do.
2. Variable "monthadd" should be named at least "monthAdd", and it is still sounds confusing what it adds.
3. Variables as "IDP", "IPV_DISQL_PERIOD" expected to be constants, is it true? In Java only constants suppose to be named in all upper cases.
4. Variable "cal" would clearer as "calendar", as "cal" could mean calculator also - ambiguous. "date1" is meaningless, the same as "cal", so when you compare if (date1 < cal) is not clear what actually is compared. Current date with something or...?
5. "IPVValidCheck" - would expect this as boolean, not String, probably because of not clear naming.
6. Lines 15, 16, 17 - badly indented code.
7. It doesn't contain "return" statement.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're going to compare a Date and a Calendar type, then you need to find something that they both express in the same way. Milliseconds seems like a good candidate.

But be careful: cal isn't always going to be initialized. What happens if IDP is 97, 98, or 99? Also, you have what looks like a bogus declaration of int cal.
 
John Morgan
Ranch Hand
Posts: 205
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:A bit off topic... Sorry for being picky.

1. Your method doing more things than method's name suggests it's suppose to do.
2. Variable "monthadd" should be named at least "monthAdd", and it is still sounds confusing what it adds.
3. Variables as "IDP", "IPV_DISQL_PERIOD" expected to be constants, is it true? In Java only constants suppose to be named in all upper cases.
4. Variable "cal" would clearer as "calendar", as "cal" could mean calculator also - ambiguous. "date1" is meaningless, the same as "cal", so when you compare if (date1 < cal) is not clear what actually is compared. Current date with something or...?
5. "IPVValidCheck" - would expect this as boolean, not String, probably because of not clear naming.
6. Lines 15, 16, 17 - badly indented code.
7. It doesn't contain "return" statement.



Okay so I cleaned up some of my code (definately one of the IF statements was in the wrong place)... Here is the updated code. The only error that I get at the moment is "The operator < is undefined for the argument type(s) java.util.Calendar, java.util.Calendar" where I have (today < ipvEndDate)
 
Ranch Hand
Posts: 57
3
Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't directly compare two types of Calendar. Try converting them into something that can be compared, such as integers.
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm looking at this code without any understanding what it is meant to do. John, I think you should clear it up for yourself too.
1. What is the single purpose of this method? I noticed that method name changed from your previous code. And other problems too:
a) Variables calendar, date1, sdf are not used.
b) Still no return statement.
c) Line 8 using variable "IPV_DISQL_PERIOD", should be "idp" it seems.
d) Numbers: 97, 98, 99 are magic numbers without any meanings.
e) You have montAdd = idp = IPV_DISQL_PERIOD, you could omit 1 unnecessary declaration/initialisation.
f) Probably would work all this better in "switch" statement rather than multiple if's, else if's.

But I'd suggest at first clearly define what singular task your method suppose to achieve and start over without fixing current issues, there are a lot of them.
 
John Morgan
Ranch Hand
Posts: 205
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) The singular purpose is to take a date that is provide to me from a database, add a certain length of time and see of that date is before todays date.
a) sorry that was Remanent of old code.
b) I was not going to add the return statement until I got the comparison working
c) fixed
d) Regardless of the value of idp it will return IPVValidCheck string. (return IPValidCheck has been added to the end of the statement)
e) fixed
f) I am stepping into this and have to follow the convention given. (not easy for someone beginning to use Java in the real world)

Liutauras Vilda wrote:I'm looking at this code without any understanding what it is meant to do. John, I think you should clear it up for yourself too.
1. What is the single purpose of this method? I noticed that method name changed from your previous code. And other problems too:
a) Variables calendar, date1, sdf are not used.
b) Still no return statement.
c) Line 8 using variable "IPV_DISQL_PERIOD", should be "idp" it seems.
d) Numbers: 97, 98, 99 are magic numbers without any meanings.
e) You have montAdd = idp = IPV_DISQL_PERIOD, you could omit 1 unnecessary declaration/initialisation.
f) Probably would work all this better in "switch" statement rather than multiple if's, else if's.

But I'd suggest at first clearly define what singular task your method suppose to achieve and start over without fixing current issues, there are a lot of them.

 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Morgan wrote:1) The singular purpose is to take a date that is provide to me from a database, add a certain length of time and see of that date is before todays date.

Great, so, get this method done first, to accomplish your stated task only.
1. Pass in "Date" from database and "certain length of time to add" as arguments.
2. Compare that with "current date" and return "true" or "false" accordingly.
3. Test method to see if it gives you an expected result.

John Morgan wrote:d) Regardless of the value of idp it will return IPVValidCheck string.

Another method to create.
1. Method should accept "disclosed period" as a parameter.
2. Define values 97, 98, 99 and give them meaningful names.
3. Make conditions to check "idp" with defined values above. Use method created above if needed. Also think about the case when non of them are met. Maybe validate before you start, so you could know you have what you want.
4. Assign appropriate string message to "IPVValidCheck" based on met condition in above step.
5. Return String.
6. Test it.

I might missed something, you'll find out soon
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The only error that I get at the moment is "The operator < is undefined for the argument type(s) java.util.Calendar, java.util.Calendar" where I have (today < ipvEndDate)


Did you see my post?
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After you check what Knute suggested to you, you could also check Java API (<- link) for Calendar and Date classes for the existing methods. You might find something useful there too.
 
John Morgan
Ranch Hand
Posts: 205
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for your help.

After struggling with it for a while I went to our senior programmer and was told to use Joda-Time which was a whole lot easier to integrate with our program.
Now I just have one little bug to figure out, even if I add 0 months the time changes based on the millisecond, and I just need to compare the dates, without worrying about the time. I am sure I will get it from the Joda-Time documentation.

The code below is for anyone who looks at this in the future.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried LocalDate?
 
John Morgan
Ranch Hand
Posts: 205
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did but it still gave me the time. The fix I found was to change line 13 with

Knute Snortum wrote:Have you tried LocalDate?

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The difference in millis even when you add 0 months is due to you creating another instance of DateTime on line 4. Try this instead:
 
reply
    Bookmark Topic Watch Topic
  • New Topic