• 0

Java Calendar or Date?


Question

Hey guys,

Basically I need some help doing this, I've been coding in Java for almost 2 years for studies so I'm not overly new to this. But basically I have a field of type Calendar (I can chage this but I'm using Calendar for now) and another field of the same type as well.

So when I construct them I want the first variable say 'date1' to be a date of my chose so tomorrow and I want 'date2' to be 'date1' plus 2 weeks or 30 days or a value of my choosing. This is the hard bit so any ideas?

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

depends how you set it up

I would have personally made date1 and date2 an object that contains, month day year time

that way you can do

date2.setDate(date.getMonth(),date.getDay() + 14,date.getYear())

whereas setDate(int, int, int) will also check if it exceeded 30-31 days depending on month, then increment the month if it has exceeded it.

thats just how i would implement it, but there are tons of other implementations also

Link to comment
Share on other sites

  • 0

GregorianCalendar is what your after. Constructing a new one will make it the time to the millisecond when it was created. From here it is easy to add units of time defined by the constants (I think they're in Calendar), so it is easy to add a day, week, year etc.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.