• Post Reply Bookmark Topic Watch Topic
  • New Topic

howto: Date arithmatic ?  RSS feed

 
achana chan
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been struggling with this bit of code I was struggling with:

I'm trying to understand how to use the following methods:

I was also trying to do date arithmetic, like this

My problem is that the cal.YEAR, cal.MONTH, cal.DECEMBER and cal.DATE, 20 are hardcoded.
My attempts in using variables have thrown compile time error.
Question : How do I substitute those hardcoded String values for variables ?
TIA:-)
 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try using GregorianCalendar. This has some functionality in it so you can add days to your date.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also take a look at a couple of JavaWorld articles:
  • Calculating Java dates
  • Working in Java time : Learn the basics of calculating elapsed time in Java

  • Also look at JavaAlmanac.com for some good examples ( Date Examples)
     
    achana chan
    Ranch Hand
    Posts: 277
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you for the pointers and your time.
    It was very much *appreciated*.
     
    achana chan
    Ranch Hand
    Posts: 277
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Boy those articles are good, really hit the spot.
    Here's the code snippet, they work well.
    With respect to performance, should I be using GregorianCalendar or .getTime() to perform day artihmatic? I used both methods here. Can someone please comment on the relative efficiency please.

    TIA :-)
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!