This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

howto: Date arithmatic ?  RSS feed

 
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:-)
 
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.
 
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 :-)
     
    I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
    Rocket Oven Kickstarter - from the trailboss
    https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!