Cameron Wilby
Cameron Wilby

Reputation: 2310

Creating a Date object with an offset in Java

I'm currently learning how to create applications for Android, but my Java is quite rusty as I'm more of a .NET person.

If in C#, I wanted to create a DateTime object with the value set to todays date plus 5 years, I could use

DateTime dt = DateTime.Now.AddYears(5);

Is there something similar to this in the Java language?

Upvotes: 2

Views: 6101

Answers (2)

mini
mini

Reputation: 545

you can use JODA time --- http://joda-time.sourceforge.net to create DateTime object.

Use plusYears method to add 5 years to it -- http://joda-time.sourceforge.net/api-release/org/joda/time/DateTime.html

 DateTime.now().plusYears(5);

Upvotes: 2

Anthony Grist
Anthony Grist

Reputation: 38345

You could use a Calendar to do the calculation:

Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, 5);
Date date = cal.getTime(); // getTime() returns a Date object

Upvotes: 7

Related Questions